1 // Tencent is pleased to support the open source community by making RapidJSON available.
2 //
3 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
4 //
5 // Licensed under the MIT License (the "License"); you may not use this file except
6 // in compliance with the License. You may obtain a copy of the License at
7 //
8 // http://opensource.org/licenses/MIT
9 //
10 // Unless required by applicable law or agreed to in writing, software distributed
11 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12 // CONDITIONS OF ANY KIND, either express or implied. See the License for the
13 // specific language governing permissions and limitations under the License.
14
15 #ifndef RAPIDJSON_INTERNAL_STRFUNC_H_
16 #define RAPIDJSON_INTERNAL_STRFUNC_H_
17
18 #include "../stream.h"
19 #include <cwchar>
20
21 RAPIDJSON_NAMESPACE_BEGIN
22 namespace internal {
23
24 //! Custom strlen() which works on different character types.
25 /*! \tparam Ch Character type (e.g. char, wchar_t, short)
26 \param s Null-terminated input string.
27 \return Number of characters in the string.
28 \note This has the same semantics as strlen(), the return value is not number of Unicode codepoints.
29 */
30 template <typename Ch>
StrLen(const Ch * s)31 inline SizeType StrLen(const Ch* s) {
32 RAPIDJSON_ASSERT(s != 0);
33 const Ch* p = s;
34 while (*p) ++p;
35 return SizeType(p - s);
36 }
37
38 template <>
StrLen(const char * s)39 inline SizeType StrLen(const char* s) {
40 return SizeType(std::strlen(s));
41 }
42
43 template <>
StrLen(const wchar_t * s)44 inline SizeType StrLen(const wchar_t* s) {
45 return SizeType(std::wcslen(s));
46 }
47
48 //! Custom strcmpn() which works on different character types.
49 /*! \tparam Ch Character type (e.g. char, wchar_t, short)
50 \param s1 Null-terminated input string.
51 \param s2 Null-terminated input string.
52 \return 0 if equal
53 */
54 template<typename Ch>
StrCmp(const Ch * s1,const Ch * s2)55 inline int StrCmp(const Ch* s1, const Ch* s2) {
56 RAPIDJSON_ASSERT(s1 != 0);
57 RAPIDJSON_ASSERT(s2 != 0);
58 while(*s1 && (*s1 == *s2)) { s1++; s2++; }
59 return static_cast<unsigned>(*s1) < static_cast<unsigned>(*s2) ? -1 : static_cast<unsigned>(*s1) > static_cast<unsigned>(*s2);
60 }
61
62 //! Returns number of code points in a encoded string.
63 template<typename Encoding>
CountStringCodePoint(const typename Encoding::Ch * s,SizeType length,SizeType * outCount)64 bool CountStringCodePoint(const typename Encoding::Ch* s, SizeType length, SizeType* outCount) {
65 RAPIDJSON_ASSERT(s != 0);
66 RAPIDJSON_ASSERT(outCount != 0);
67 GenericStringStream<Encoding> is(s);
68 const typename Encoding::Ch* end = s + length;
69 SizeType count = 0;
70 while (is.src_ < end) {
71 unsigned codepoint;
72 if (!Encoding::Decode(is, &codepoint))
73 return false;
74 count++;
75 }
76 *outCount = count;
77 return true;
78 }
79
80 } // namespace internal
81 RAPIDJSON_NAMESPACE_END
82
83 #endif // RAPIDJSON_INTERNAL_STRFUNC_H_
84