OLD | NEW |
1 // Copyright 1999-2005 The RE2 Authors. All Rights Reserved. | 1 // Copyright 1999-2005 The RE2 Authors. All Rights Reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 #include "util/util.h" | 5 #include "util/util.h" |
6 #include "re2/stringpiece.h" | 6 #include "re2/stringpiece.h" |
7 | 7 |
8 namespace re2 { | 8 namespace re2 { |
9 | 9 |
10 // ---------------------------------------------------------------------- | 10 // ---------------------------------------------------------------------- |
11 // CEscapeString() | 11 // CEscapeString() |
12 // Copies 'src' to 'dest', escaping dangerous characters using | 12 // Copies 'src' to 'dest', escaping dangerous characters using |
13 // C-style escape sequences. 'src' and 'dest' should not overlap. | 13 // C-style escape sequences. 'src' and 'dest' should not overlap. |
14 // Returns the number of bytes written to 'dest' (not including the \0) | 14 // Returns the number of bytes written to 'dest' (not including the \0) |
15 // or -1 if there was insufficient space. | 15 // or -1 if there was insufficient space. |
16 // ---------------------------------------------------------------------- | 16 // ---------------------------------------------------------------------- |
17 int CEscapeString(const char* src, int src_len, char* dest, | 17 int CEscapeString(const char* src, int src_len, char* dest, |
18 int dest_len) { | 18 int dest_len) { |
19 const char* src_end = src + src_len; | 19 const char* src_end = src + src_len; |
20 int used = 0; | 20 int used = 0; |
21 | 21 |
22 for (; src < src_end; src++) { | 22 for (; src < src_end; src++) { |
23 if (dest_len - used < 2) // Need space for two letter escape | 23 if (dest_len - used < 2) // space for two-character escape |
24 return -1; | 24 return -1; |
25 | 25 |
26 unsigned char c = *src; | 26 unsigned char c = *src; |
27 switch (c) { | 27 switch (c) { |
28 case '\n': dest[used++] = '\\'; dest[used++] = 'n'; break; | 28 case '\n': dest[used++] = '\\'; dest[used++] = 'n'; break; |
29 case '\r': dest[used++] = '\\'; dest[used++] = 'r'; break; | 29 case '\r': dest[used++] = '\\'; dest[used++] = 'r'; break; |
30 case '\t': dest[used++] = '\\'; dest[used++] = 't'; break; | 30 case '\t': dest[used++] = '\\'; dest[used++] = 't'; break; |
31 case '\"': dest[used++] = '\\'; dest[used++] = '\"'; break; | 31 case '\"': dest[used++] = '\\'; dest[used++] = '\"'; break; |
32 case '\'': dest[used++] = '\\'; dest[used++] = '\''; break; | 32 case '\'': dest[used++] = '\\'; dest[used++] = '\''; break; |
33 case '\\': dest[used++] = '\\'; dest[used++] = '\\'; break; | 33 case '\\': dest[used++] = '\\'; dest[used++] = '\\'; break; |
34 default: | 34 default: |
35 // Note that if we emit \xNN and the src character after that is a hex | 35 // Note that if we emit \xNN and the src character after that is a hex |
36 // digit then that digit must be escaped too to prevent it being | 36 // digit then that digit must be escaped too to prevent it being |
37 // interpreted as part of the character code by C. | 37 // interpreted as part of the character code by C. |
38 if (c < ' ' || c > '~') { | 38 if (c < ' ' || c > '~') { |
39 if (dest_len - used < 4) // need space for 4 letter escape | 39 if (dest_len - used < 5) // space for four-character escape + \0 |
40 return -1; | 40 return -1; |
41 sprintf(dest + used, "\\%03o", c); | 41 #if !defined(_WIN32) |
| 42 snprintf(dest + used, 5, "\\%03o", c); |
| 43 #else |
| 44 // On Windows, the function takes 4+VA arguments, not 3+VA. With an |
| 45 // array, the buffer size will be inferred, but not with a pointer. |
| 46 snprintf(dest + used, 5, _TRUNCATE, "\\%03o", c); |
| 47 #endif |
42 used += 4; | 48 used += 4; |
43 } else { | 49 } else { |
44 dest[used++] = c; break; | 50 dest[used++] = c; break; |
45 } | 51 } |
46 } | 52 } |
47 } | 53 } |
48 | 54 |
49 if (dest_len - used < 1) // make sure that there is room for \0 | 55 if (dest_len - used < 1) // make sure that there is room for \0 |
50 return -1; | 56 return -1; |
51 | 57 |
52 dest[used] = '\0'; // doesn't count towards return value though | 58 dest[used] = '\0'; // doesn't count towards return value though |
53 return used; | 59 return used; |
54 } | 60 } |
55 | 61 |
56 | 62 |
57 // ---------------------------------------------------------------------- | 63 // ---------------------------------------------------------------------- |
58 // CEscape() | 64 // CEscape() |
59 // Copies 'src' to result, escaping dangerous characters using | 65 // Copies 'src' to result, escaping dangerous characters using |
60 // C-style escape sequences. 'src' and 'dest' should not overlap. | 66 // C-style escape sequences. 'src' and 'dest' should not overlap. |
61 // ---------------------------------------------------------------------- | 67 // ---------------------------------------------------------------------- |
62 string CEscape(const StringPiece& src) { | 68 string CEscape(const StringPiece& src) { |
63 const int dest_length = src.size() * 4 + 1; // Maximum possible expansion | 69 const int dest_length = src.size() * 4 + 1; // Maximum possible expansion |
64 char* dest = new char[dest_length]; | 70 char* dest = new char[dest_length]; |
65 const int len = CEscapeString(src.data(), src.size(), | 71 const int len = CEscapeString(src.data(), src.size(), |
66 dest, dest_length); | 72 dest, dest_length); |
67 string s = string(dest, len); | 73 string s = string(dest, len); |
68 delete[] dest; | 74 delete[] dest; |
69 return s; | 75 return s; |
70 } | 76 } |
71 | 77 |
72 string PrefixSuccessor(const StringPiece& prefix) { | 78 string PrefixSuccessor(const StringPiece& prefix) { |
73 // We can increment the last character in the string and be done | 79 // We can increment the last character in the string and be done |
74 // unless that character is 255, in which case we have to erase the | 80 // unless that character is 255, in which case we have to erase the |
75 // last character and increment the previous character, unless that | 81 // last character and increment the previous character, unless that |
76 // is 255, etc. If the string is empty or consists entirely of | 82 // is 255, etc. If the string is empty or consists entirely of |
77 // 255's, we just return the empty string. | 83 // 255's, we just return the empty string. |
78 bool done = false; | 84 bool done = false; |
79 string limit(prefix.data(), prefix.size()); | 85 string limit(prefix.data(), prefix.size()); |
80 int index = limit.length() - 1; | 86 int index = static_cast<int>(limit.size()) - 1; |
81 while (!done && index >= 0) { | 87 while (!done && index >= 0) { |
82 if ((limit[index]&255) == 255) { | 88 if ((limit[index]&255) == 255) { |
83 limit.erase(index); | 89 limit.erase(index); |
84 index--; | 90 index--; |
85 } else { | 91 } else { |
86 limit[index]++; | 92 limit[index]++; |
87 done = true; | 93 done = true; |
88 } | 94 } |
89 } | 95 } |
90 if (!done) { | 96 if (!done) { |
91 return ""; | 97 return ""; |
92 } else { | 98 } else { |
93 return limit; | 99 return limit; |
94 } | 100 } |
95 } | 101 } |
96 | 102 |
97 } // namespace re2 | 103 } // namespace re2 |
OLD | NEW |