2 +----------------------------------------------------------------------+
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"
20 #include <cstring> // memcpy
23 #include "hphp/util/string-vsnprintf.h"
30 void replaceAll(string
&s
, const char *from
, const char *to
) {
31 assert(from
&& *from
);
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
);
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
) {
48 ret
.reserve(s
.size());
49 for (auto const c
: s
) {
55 std::string
toUpper(folly::StringPiece s
) {
57 ret
.reserve(s
.size());
58 for (auto const c
: s
) {
64 std::string
getIdentifier(const std::string
&fileName
) {
65 string ret
= "hphp_" + fileName
;
66 replaceAll(ret
, "/", "__");
67 replaceAll(ret
, ".", "__");
68 replaceAll(ret
, "-", "__");
72 std::string
escapeStringForCPP(const char *input
, int len
,
73 bool* binary
/* = NULL */) {
74 if (binary
) *binary
= false;
76 ret
.reserve((len
<< 1) + 2);
77 for (int i
= 0; i
< len
; i
++) {
78 unsigned char ch
= input
[i
];
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
95 // output in octal notation
97 snprintf(buf
, sizeof(buf
), "\\%03o", ch
);
106 std::string
escapeStringForPHP(const char *input
, int len
) {
108 output
.reserve((len
<< 1) + 2);
110 for (int i
= 0; i
< len
; i
++) {
111 unsigned char ch
= input
[i
];
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;
125 replaceAll(output
, ".''.", ".");
126 replaceAll(output
, "''.", "");
127 replaceAll(output
, ".''", "");
128 replaceAll(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
);
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';
147 void string_printf(std::string
&msg
, const char *fmt
, ...) {
150 string_vsnprintf(msg
, fmt
, 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
!= '(') {