declare_folded_class NO LONGER _in_file
[hiphop-php.git] / hphp / util / text-util.cpp
blobcffc0ab58b9541e862428e33501ef1f172f9711a
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #include "hphp/util/text-util.h"
19 #include <cassert>
20 #include <cstring> // memcpy
21 #include <string>
22 #include <vector>
23 #include "hphp/util/string-vsnprintf.h"
25 namespace HPHP {
27 using std::string;
28 using std::vector;
30 void replaceAll(string &s, const char *from, const char *to) {
31 assert(from && *from);
32 assert(to);
34 string::size_type pos = s.find(from);
35 if (pos == string::npos) return;
37 string::size_type lenFrom = strlen(from);
38 string::size_type lenTo = strlen(to);
40 do {
41 s.replace(pos, lenFrom, to);
42 pos = s.find(from, pos + lenTo);
43 } while (pos != string::npos);
46 std::string toLower(folly::StringPiece s) {
47 std::string ret;
48 ret.reserve(s.size());
49 for (auto const c : s) {
50 ret += tolower(c);
52 return ret;
55 std::string toUpper(folly::StringPiece s) {
56 std::string ret;
57 ret.reserve(s.size());
58 for (auto const c : s) {
59 ret += toupper(c);
61 return ret;
64 std::string getIdentifier(const std::string &fileName) {
65 string ret = "hphp_" + fileName;
66 replaceAll(ret, "/", "__");
67 replaceAll(ret, ".", "__");
68 replaceAll(ret, "-", "__");
69 return ret;
72 std::string escapeStringForCPP(const char *input, int len,
73 bool* binary /* = NULL */) {
74 if (binary) *binary = false;
75 string ret;
76 ret.reserve((len << 1) + 2);
77 for (int i = 0; i < len; i++) {
78 unsigned char ch = input[i];
79 switch (ch) {
80 case '\n': ret += "\\n"; break;
81 case '\r': ret += "\\r"; break;
82 case '\t': ret += "\\t"; break;
83 case '\a': ret += "\\a"; break;
84 case '\b': ret += "\\b"; break;
85 case '\f': ret += "\\f"; break;
86 case '\v': ret += "\\v"; break;
87 case '\0': ret += "\\000"; if (binary) *binary = true; break;
88 case '\"': ret += "\\\""; break;
89 case '\\': ret += "\\\\"; break;
90 case '?': ret += "\\?"; break; // avoiding trigraph errors
91 default:
92 if (isprint(ch)) {
93 ret += ch;
94 } else {
95 // output in octal notation
96 char buf[10];
97 snprintf(buf, sizeof(buf), "\\%03o", ch);
98 ret += buf;
100 break;
103 return ret;
106 std::string escapeStringForPHP(const char *input, int len) {
107 string output;
108 output.reserve((len << 1) + 2);
109 output = "'";
110 for (int i = 0; i < len; i++) {
111 unsigned char ch = input[i];
112 switch (ch) {
113 case '\n': output += "'.\"\\n\".'"; break;
114 case '\r': output += "'.\"\\r\".'"; break;
115 case '\t': output += "'.\"\\t\".'"; break;
116 case '\'': output += "'.\"'\".'"; break;
117 case '\\': output += "'.\"\\\\\".'"; break;
118 case '\0': output += "'.\"\\0\".'"; break;
119 default:
120 output += ch;
121 break;
124 output += "'";
125 replaceAll(output, ".''.", ".");
126 replaceAll(output, "''.", "");
127 replaceAll(output, ".''", "");
128 replaceAll(output, "\".\"", "");
129 return output;
132 const void *buffer_duplicate(const void *src, size_t size) {
133 char *s = (char *)malloc(size + 1); // '\0' in the end
134 memcpy(s, src, size);
135 s[size] = '\0';
136 return s;
139 const void *buffer_append(const void *buf1, size_t size1,
140 const void *buf2, size_t size2) {
141 char *s = (char *)realloc(const_cast<void *>(buf1), size1 + size2 + 1);
142 memcpy((char *)s + size1, buf2, size2);
143 s[size1 + size2] = '\0';
144 return s;
147 void string_printf(std::string &msg, const char *fmt, ...) {
148 va_list ap;
149 va_start(ap, fmt);
150 string_vsnprintf(msg, fmt, ap);
151 va_end(ap);
154 std::string format_pattern(const std::string &pattern, bool prefixSlash) {
155 if (pattern.empty()) return pattern;
157 std::string ret = "#";
158 for (unsigned int i = 0; i < pattern.size(); i++) {
159 char ch = pattern[i];
161 // apache rewrite rules don't require initial slash
162 if (prefixSlash && i == 0 && ch == '^') {
163 char ch1 = pattern[1];
164 if (ch1 != '/' && ch1 != '(') {
165 ret += "^/";
166 continue;
170 if (ch == '#') {
171 ret += "\\#";
172 } else {
173 ret += ch;
176 ret += '#';
177 return ret;
180 } // namespace HPHP