Revert "Fix bug in mysql_next_result's return value that confused "done" with "error""
[hiphop-php.git] / hphp / runtime / base / zend_string.h
blobb73af81323a01e1cd0c5840a8d112b433c6a9496
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-2013 Facebook, Inc. (http://www.facebook.com) |
6 | Copyright (c) 1998-2010 Zend Technologies Ltd. (http://www.zend.com) |
7 +----------------------------------------------------------------------+
8 | This source file is subject to version 2.00 of the Zend license, |
9 | that is bundled with this package in the file LICENSE, and is |
10 | available through the world-wide-web at the following url: |
11 | http://www.zend.com/license/2_00.txt. |
12 | If you did not receive a copy of the Zend license and are unable to |
13 | obtain it through the world-wide-web, please send a note to |
14 | license@zend.com so we can mail you a copy immediately. |
15 +----------------------------------------------------------------------+
18 #ifndef incl_HPHP_ZEND_STRING_H_
19 #define incl_HPHP_ZEND_STRING_H_
21 #include "hphp/util/base.h"
22 #include "hphp/util/zend/zend_string.h"
23 #include "hphp/runtime/base/complex_types.h"
24 #include "hphp/runtime/base/string_buffer.h"
26 namespace HPHP {
27 ///////////////////////////////////////////////////////////////////////////////
28 /**
29 * Low-level string functions PHP uses.
31 * 1. If a function returns a char *, it has malloc-ed a new string and it's
32 * caller's responsibility to free it.
34 * 2. If a function takes "int &len" right after the 1st string parameter, it
35 * is input string's length, and in return, it's return string's length.
37 * 3. All functions work with binary strings and all returned strings are
38 * NULL terminated, regardless of whether it's a binary string.
42 * Copy src to string dst of size siz. At most siz-1 characters
43 * will be copied. Always NUL terminates (unless siz == 0).
44 * Returns strlen(src); if retval >= siz, truncation occurred.
46 int string_copy(char *dst, const char *src, int siz);
48 /**
49 * Compare two binary strings.
51 inline int string_strcmp(const char *s1, int len1, const char *s2, int len2) {
52 int minlen = len1 < len2 ? len1 : len2;
53 int retval;
55 retval = memcmp(s1, s2, minlen);
56 if (!retval) {
57 return (len1 - len2);
59 return retval;
61 /**
62 * Compare two binary strings of the first n bytes.
64 inline int string_strncmp(const char *s1, int len1, const char *s2, int len2,
65 int len) {
66 int minlen = len1 < len2 ? len1 : len2;
67 int retval;
69 if (len < minlen) {
70 if (UNLIKELY(len < 0)) len = 0;
71 minlen = len;
73 retval = memcmp(s1, s2, minlen);
74 if (!retval) {
75 return (len < len1 ? len : len1) - (len < len2 ? len : len2);
76 } else {
77 return retval;
80 /**
81 * Compare two binary strings of the first n bytes, ignore case.
83 inline int string_strncasecmp(const char *s1, int len1,
84 const char *s2, int len2, int len) {
85 int minlen = len1 < len2 ? len1 : len2;
86 int c1, c2;
88 if (len < minlen) {
89 if (UNLIKELY(len < 0)) len = 0;
90 minlen = len;
92 while (minlen--) {
93 c1 = tolower((int)*(unsigned char *)s1++);
94 c2 = tolower((int)*(unsigned char *)s2++);
95 if (c1 != c2) {
96 return c1 - c2;
99 return (len < len1 ? len : len1) - (len < len2 ? len : len2);
102 * Concatenate two into one.
104 char *string_concat(const char *s1, int len1, const char *s2, int len2,
105 int &len);
108 * Compare strings.
110 int string_ncmp(const char *s1, const char *s2, int len);
111 int string_natural_cmp(char const *a, size_t a_len,
112 char const *b, size_t b_len, int fold_case);
115 * Changing string's cases. Return's length is always the same as "len".
117 char *string_to_case(const char *s, int len, int (*tocase)(int));
118 char *string_to_case_first(const char *s, int len, int (*tocase)(int));
119 char *string_to_case_words(const char *s, int len, int (*tocase)(int));
121 #define string_to_upper(s,len) string_to_case((s), (len), toupper)
122 #define string_to_upper_first(s, len) string_to_case_first((s), (len), toupper)
123 #define string_to_upper_words(s, len) string_to_case_words((s), (len), toupper)
125 #define string_to_lower(s,len) string_to_case((s), (len), tolower)
126 #define string_to_lower_first(s, len) string_to_case_first((s), (len), tolower)
127 #define string_to_lower_words(s, len) string_to_case_words((s), (len), tolower)
131 * Trim a string by removing characters in the specified charlist.
133 * mode 1 : trim left
134 * mode 2 : trim right
135 * mode 3 : trim left and right
137 char *string_trim(const char *s, int &len,
138 const char *charlist, int charlistlen, int mode);
141 * Pad a string with pad_string to pad_length. "len" is
142 * input string's length, and in return, it's trimmed string's length. pad_type
143 * can be k_STR_PAD_RIGHT, k_STR_PAD_LEFT or k_STR_PAD_BOTH.
145 char *string_pad(const char *input, int &len, int pad_length,
146 const char *pad_string, int pad_str_len, int pad_type);
149 * Get a substring of input from "start" position with specified length.
150 * "start" and "length" can be negative and refer to PHP's doc for meanings.
152 char *string_substr(const char *s, int &len, int start, int length,
153 bool nullable);
156 * Find a character or substring and return it's position (or -1 if not found).
158 int string_find(const char *input, int len, char ch, int pos,
159 bool case_sensitive);
160 int string_rfind(const char *input, int len, char ch, int pos,
161 bool case_sensitive);
162 int string_find(const char *input, int len, const char *s, int s_len,
163 int pos, bool case_sensitive);
164 int string_rfind(const char *input, int len, const char *s, int s_len,
165 int pos, bool case_sensitive);
167 const char *string_memnstr(const char *haystack, const char *needle,
168 int needle_len, const char *end);
171 * Replace specified substring or search string with specified replacement.
173 char *string_replace(const char *s, int &len, int start, int length,
174 const char *replacement, int len_repl);
175 char *string_replace(const char *input, int &len,
176 const char *search, int len_search,
177 const char *replacement, int len_replace,
178 int &count, bool case_sensitive);
181 * Reverse, repeat or shuffle a string.
183 char *string_reverse(const char *s, int len);
184 char *string_repeat(const char *s, int &len, int count);
185 char *string_shuffle(const char *str, int len);
186 char *string_chunk_split(const char *src, int &srclen, const char *end,
187 int endlen, int chunklen);
190 * Strip HTML and PHP tags.
192 char *string_strip_tags(const char *s, int &len, const char *allow,
193 int allow_len, bool allow_tag_spaces);
196 * Wrap text on word breaks.
198 char *string_wordwrap(const char *text, int &textlen, int linelength,
199 const char *breakchar, int breakcharlen, bool docut);
202 * Encoding/decoding strings according to certain formats.
204 char *string_addcslashes(const char *str, int &length, const char *what,
205 int wlength);
206 char *string_stripcslashes(const char *input, int &nlen);
207 char *string_addslashes(const char *str, int &length);
208 char *string_stripslashes(const char *input, int &l);
209 char *string_quotemeta(const char *input, int &len);
210 char *string_quoted_printable_encode(const char *input, int &len);
211 char *string_quoted_printable_decode(const char *input, int &len, bool is_q);
212 char *string_uuencode(const char *src, int src_len, int &dest_len);
213 char *string_uudecode(const char *src, int src_len, int &dest_len);
214 char *string_base64_encode(const char *input, int &len);
215 char *string_base64_decode(const char *input, int &len, bool strict);
216 char *string_escape_shell_arg(const char *str);
217 char *string_escape_shell_cmd(const char *str);
218 std::string string_cplus_escape(const char *s, int len);
221 * Convert between strings and numbers.
223 inline bool string_validate_base(int base) {
224 return (2 <= base && base <= 36);
226 char *string_hex2bin(const char *input, int &len);
227 Variant string_base_to_numeric(const char *s, int len, int base);
228 char *string_long_to_base(unsigned long value, int base);
229 char *string_numeric_to_base(CVarRef value, int base);
232 * Translates characters in str_from into characters in str_to one by one,
233 * assuming str_from and str_to have the same length of "trlen".
235 void string_translate(char *str, int len, const char *str_from,
236 const char *str_to, int trlen);
239 * Formatting.
241 char *string_money_format(const char *format, double value);
243 char *string_number_format(double d, int dec, char dec_point,
244 char thousand_sep);
247 * Similarity and other properties of strings.
249 int string_levenshtein(const char *s1, int l1, const char *s2, int l2,
250 int cost_ins, int cost_rep, int cost_del);
251 int string_similar_text(const char *t1, int len1,
252 const char *t2, int len2, float *percent);
253 char *string_soundex(const char *str);
255 char *string_metaphone(const char *input, int word_len, long max_phonemes,
256 int traditional);
259 * Locale strings.
261 char *string_convert_cyrillic_string(const char *input, int length,
262 char from, char to);
263 char *string_convert_hebrew_string(const char *str, int &str_len,
264 int max_chars_per_line,
265 int convert_newlines);
267 ///////////////////////////////////////////////////////////////////////////////
268 // helpers
271 * Calculates and adjusts "start" and "length" according to string's length.
272 * This function determines how those two parameters are interpreted in varies
273 * substr-related functions.
275 * The parameter strict controls whether to disallow the empty sub-string
276 * after the end.
278 bool string_substr_check(int len, int &f, int &l, bool strict = true);
281 * Fills a 256-byte bytemask with input. You can specify a range like 'a..z',
282 * it needs to be incrementing. This function determines how "charlist"
283 * parameters are interpreted in varies functions that take a list of
284 * characters.
286 void string_charmask(const char *input, int len, char *mask);
288 ///////////////////////////////////////////////////////////////////////////////
289 // mac doesn't have memrchr
291 #if defined(__APPLE__)
292 void *memrchr(const void *s, int c, size_t n);
293 #endif
295 ///////////////////////////////////////////////////////////////////////////////
298 #endif // incl_HPHP_ZEND_STRING_H_