Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: third_party/re2/util/strutil.cc

Issue 1530113002: Revert of Update re2 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/re2/util/stringprintf.cc ('k') | third_party/re2/util/test.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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) // space for two-character escape 23 if (dest_len - used < 2) // Need space for two letter 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 < 5) // space for four-character escape + \0 39 if (dest_len - used < 4) // need space for 4 letter escape
40 return -1; 40 return -1;
41 #if !defined(_WIN32) 41 sprintf(dest + used, "\\%03o", c);
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
48 used += 4; 42 used += 4;
49 } else { 43 } else {
50 dest[used++] = c; break; 44 dest[used++] = c; break;
51 } 45 }
52 } 46 }
53 } 47 }
54 48
55 if (dest_len - used < 1) // make sure that there is room for \0 49 if (dest_len - used < 1) // make sure that there is room for \0
56 return -1; 50 return -1;
57 51
58 dest[used] = '\0'; // doesn't count towards return value though 52 dest[used] = '\0'; // doesn't count towards return value though
59 return used; 53 return used;
60 } 54 }
61 55
62 56
63 // ---------------------------------------------------------------------- 57 // ----------------------------------------------------------------------
64 // CEscape() 58 // CEscape()
65 // Copies 'src' to result, escaping dangerous characters using 59 // Copies 'src' to result, escaping dangerous characters using
66 // C-style escape sequences. 'src' and 'dest' should not overlap. 60 // C-style escape sequences. 'src' and 'dest' should not overlap.
67 // ---------------------------------------------------------------------- 61 // ----------------------------------------------------------------------
68 string CEscape(const StringPiece& src) { 62 string CEscape(const StringPiece& src) {
69 const int dest_length = src.size() * 4 + 1; // Maximum possible expansion 63 const int dest_length = src.size() * 4 + 1; // Maximum possible expansion
70 char* dest = new char[dest_length]; 64 char* dest = new char[dest_length];
71 const int len = CEscapeString(src.data(), src.size(), 65 const int len = CEscapeString(src.data(), src.size(),
72 dest, dest_length); 66 dest, dest_length);
73 string s = string(dest, len); 67 string s = string(dest, len);
74 delete[] dest; 68 delete[] dest;
75 return s; 69 return s;
76 } 70 }
77 71
78 string PrefixSuccessor(const StringPiece& prefix) { 72 string PrefixSuccessor(const StringPiece& prefix) {
79 // We can increment the last character in the string and be done 73 // We can increment the last character in the string and be done
80 // unless that character is 255, in which case we have to erase the 74 // unless that character is 255, in which case we have to erase the
81 // last character and increment the previous character, unless that 75 // last character and increment the previous character, unless that
82 // is 255, etc. If the string is empty or consists entirely of 76 // is 255, etc. If the string is empty or consists entirely of
83 // 255's, we just return the empty string. 77 // 255's, we just return the empty string.
84 bool done = false; 78 bool done = false;
85 string limit(prefix.data(), prefix.size()); 79 string limit(prefix.data(), prefix.size());
86 int index = static_cast<int>(limit.size()) - 1; 80 int index = limit.length() - 1;
87 while (!done && index >= 0) { 81 while (!done && index >= 0) {
88 if ((limit[index]&255) == 255) { 82 if ((limit[index]&255) == 255) {
89 limit.erase(index); 83 limit.erase(index);
90 index--; 84 index--;
91 } else { 85 } else {
92 limit[index]++; 86 limit[index]++;
93 done = true; 87 done = true;
94 } 88 }
95 } 89 }
96 if (!done) { 90 if (!done) {
97 return ""; 91 return "";
98 } else { 92 } else {
99 return limit; 93 return limit;
100 } 94 }
101 } 95 }
102 96
103 } // namespace re2 97 } // namespace re2
OLDNEW
« no previous file with comments | « third_party/re2/util/stringprintf.cc ('k') | third_party/re2/util/test.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698