This source file includes following definitions.
- CEscapeString
- CEscape
- PrefixSuccessor
#include "util/util.h"
#include "re2/stringpiece.h"
namespace re2 {
int CEscapeString(const char* src, int src_len, char* dest,
int dest_len) {
const char* src_end = src + src_len;
int used = 0;
for (; src < src_end; src++) {
if (dest_len - used < 2)
return -1;
unsigned char c = *src;
switch (c) {
case '\n': dest[used++] = '\\'; dest[used++] = 'n'; break;
case '\r': dest[used++] = '\\'; dest[used++] = 'r'; break;
case '\t': dest[used++] = '\\'; dest[used++] = 't'; break;
case '\"': dest[used++] = '\\'; dest[used++] = '\"'; break;
case '\'': dest[used++] = '\\'; dest[used++] = '\''; break;
case '\\': dest[used++] = '\\'; dest[used++] = '\\'; break;
default:
if (c < ' ' || c > '~') {
if (dest_len - used < 4)
return -1;
sprintf(dest + used, "\\%03o", c);
used += 4;
} else {
dest[used++] = c; break;
}
}
}
if (dest_len - used < 1)
return -1;
dest[used] = '\0';
return used;
}
string CEscape(const StringPiece& src) {
const int dest_length = src.size() * 4 + 1;
char* dest = new char[dest_length];
const int len = CEscapeString(src.data(), src.size(),
dest, dest_length);
string s = string(dest, len);
delete[] dest;
return s;
}
string PrefixSuccessor(const StringPiece& prefix) {
bool done = false;
string limit(prefix.data(), prefix.size());
int index = limit.length() - 1;
while (!done && index >= 0) {
if ((limit[index]&255) == 255) {
limit.erase(index);
index--;
} else {
limit[index]++;
done = true;
}
}
if (!done) {
return "";
} else {
return limit;
}
}
}