2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present 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 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_ZEND_ZEND_STRING_H_
18 #define incl_HPHP_ZEND_ZEND_STRING_H_
25 #include <folly/Range.h>
28 //////////////////////////////////////////////////////////////////////
31 * Low-level string functions PHP uses.
33 * 1. If a function returns a char *, it has malloc-ed a new string and it's
34 * caller's responsibility to free it.
36 * 2. If a function takes "int &len" right after the 1st string parameter, it
37 * is input string's length, and in return, it's return string's length.
39 * 3. All functions work with binary strings and all returned strings are
40 * NULL terminated, regardless of whether it's a binary string.
44 * Copy src to string dst of size siz. At most siz-1 characters
45 * will be copied. Always NUL terminates (unless siz == 0).
46 * Returns strlen(src); if retval >= siz, truncation occurred.
48 int string_copy(char *dst
, const char *src
, int siz
);
51 * Compare two binary strings.
53 inline int string_strcmp(const char *s1
, int len1
, const char *s2
, int len2
) {
54 int minlen
= len1
< len2
? len1
: len2
;
57 retval
= memcmp(s1
, s2
, minlen
);
62 return (retval
> 0) - (retval
< 0);
65 * Compare two binary strings of the first n bytes.
67 inline int string_strncmp(const char *s1
, int len1
, const char *s2
, int len2
,
69 int minlen
= len1
< len2
? len1
: len2
;
73 if (UNLIKELY(len
< 0)) len
= 0;
76 retval
= memcmp(s1
, s2
, minlen
);
78 return (len
< len1
? len
: len1
) - (len
< len2
? len
: len2
);
84 * Compare two binary strings of the first n bytes, ignore case.
86 inline int string_strncasecmp(const char *s1
, int len1
,
87 const char *s2
, int len2
, int len
) {
88 int minlen
= len1
< len2
? len1
: len2
;
92 if (UNLIKELY(len
< 0)) len
= 0;
96 c1
= tolower((int)*(unsigned char *)s1
++);
97 c2
= tolower((int)*(unsigned char *)s2
++);
102 return (len
< len1
? len
: len1
) - (len
< len2
? len
: len2
);
108 int string_ncmp(const char *s1
, const char *s2
, int len
);
109 int string_natural_cmp(char const *a
, size_t a_len
,
110 char const *b
, size_t b_len
, int fold_case
);
114 * Duplicate a binary string. Note that NULL termination is needed even for
115 * a binary string, because String class only wraps such a "safe" one that can
116 * work with any functions that takes a C-string.
118 inline char *string_duplicate(const char *s
, int len
) {
119 char *ret
= (char *)malloc(len
+ 1);
128 char *string_rot13(const char *input
, int len
);
129 int string_crc32(const char *p
, int len
);
130 char *string_crypt(const char *key
, const char *salt
);
131 char *string_sha1(const char *arg
, int arg_len
, bool raw
, int &out_len
);
134 Md5Digest(const char* s
, int len
);
138 std::string
string_md5(folly::StringPiece
);
139 std::string
string_sha1(folly::StringPiece
);
142 * Convert input[len] to a malloced, nul-terminated, lowercase, hex string
144 char *string_bin2hex(const char *input
, int &len
);
145 char *string_bin2hex(const char* input
, int len
, char* output
);
147 //////////////////////////////////////////////////////////////////////