|
1 #include <wchar.h> |
|
2 |
|
3 namespace base { |
|
4 |
|
5 bool IsWprintfFormatPortable(const wchar_t* format) { |
|
6 for (const wchar_t* position = format; *position != '\0'; ++position) { |
|
7 if (*position == '%') { |
|
8 bool in_specification = true; |
|
9 bool modifier_l = false; |
|
10 while (in_specification) { |
|
11 // Eat up characters until reaching a known specifier. |
|
12 if (*++position == '\0') { |
|
13 // The format string ended in the middle of a specification. Call |
|
14 // it portable because no unportable specifications were found. The |
|
15 // string is equally broken on all platforms. |
|
16 return true; |
|
17 } |
|
18 |
|
19 if (*position == 'l') { |
|
20 // 'l' is the only thing that can save the 's' and 'c' specifiers. |
|
21 modifier_l = true; |
|
22 } else if (((*position == 's' || *position == 'c') && !modifier_l) || |
|
23 *position == 'S' || *position == 'C' || *position == 'F' || |
|
24 *position == 'D' || *position == 'O' || *position == 'U') { |
|
25 // Not portable. |
|
26 return false; |
|
27 } |
|
28 |
|
29 if (wcschr(L"diouxXeEfgGaAcspn%", *position)) { |
|
30 // Portable, keep scanning the rest of the format string. |
|
31 in_specification = false; |
|
32 } |
|
33 } |
|
34 } |
|
35 } |
|
36 |
|
37 return true; |
|
38 } |
|
39 |
|
40 } // namespace base |
|
41 |