Support is and as
[hiphop-php.git] / hphp / zend / zend-string.h
blob828e6a528c8b8aa6c1b4c15bb854c33254d15a61
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
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_
20 #include <cstdint>
21 #include <cstdlib>
22 #include <cstring>
23 #include <string>
25 #include <folly/Range.h>
27 namespace HPHP {
28 //////////////////////////////////////////////////////////////////////
30 /**
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);
50 /**
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;
55 int retval;
57 retval = memcmp(s1, s2, minlen);
58 if (!retval) {
59 return (len1 - len2);
62 return (retval > 0) - (retval < 0);
64 /**
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,
68 int len) {
69 int minlen = len1 < len2 ? len1 : len2;
70 int retval;
72 if (len < minlen) {
73 if (UNLIKELY(len < 0)) len = 0;
74 minlen = len;
76 retval = memcmp(s1, s2, minlen);
77 if (!retval) {
78 return (len < len1 ? len : len1) - (len < len2 ? len : len2);
79 } else {
80 return retval;
83 /**
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;
89 int c1, c2;
91 if (len < minlen) {
92 if (UNLIKELY(len < 0)) len = 0;
93 minlen = len;
95 while (minlen--) {
96 c1 = tolower((int)*(unsigned char *)s1++);
97 c2 = tolower((int)*(unsigned char *)s2++);
98 if (c1 != c2) {
99 return c1 - c2;
102 return (len < len1 ? len : len1) - (len < len2 ? len : len2);
106 * Compare strings.
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);
120 memcpy(ret, s, len);
121 ret[len] = '\0';
122 return ret;
126 * Hashing a string.
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);
133 struct Md5Digest {
134 Md5Digest(const char* s, int len);
135 uint8_t digest[16];
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 //////////////////////////////////////////////////////////////////////
150 #endif