Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 10:28:08

0001 // Tencent is pleased to support the open source community by making RapidJSON available.
0002 // 
0003 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
0004 //
0005 // Licensed under the MIT License (the "License"); you may not use this file except
0006 // in compliance with the License. You may obtain a copy of the License at
0007 //
0008 // http://opensource.org/licenses/MIT
0009 //
0010 // Unless required by applicable law or agreed to in writing, software distributed 
0011 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
0012 // CONDITIONS OF ANY KIND, either express or implied. See the License for the 
0013 // specific language governing permissions and limitations under the License.
0014 
0015 #ifndef RAPIDJSON_INTERNAL_STRFUNC_H_
0016 #define RAPIDJSON_INTERNAL_STRFUNC_H_
0017 
0018 #include "../stream.h"
0019 #include <cwchar>
0020 
0021 RAPIDJSON_NAMESPACE_BEGIN
0022 namespace internal {
0023 
0024 //! Custom strlen() which works on different character types.
0025 /*! \tparam Ch Character type (e.g. char, wchar_t, short)
0026     \param s Null-terminated input string.
0027     \return Number of characters in the string. 
0028     \note This has the same semantics as strlen(), the return value is not number of Unicode codepoints.
0029 */
0030 template <typename Ch>
0031 inline SizeType StrLen(const Ch* s) {
0032     RAPIDJSON_ASSERT(s != 0);
0033     const Ch* p = s;
0034     while (*p) ++p;
0035     return SizeType(p - s);
0036 }
0037 
0038 template <>
0039 inline SizeType StrLen(const char* s) {
0040     return SizeType(std::strlen(s));
0041 }
0042 
0043 template <>
0044 inline SizeType StrLen(const wchar_t* s) {
0045     return SizeType(std::wcslen(s));
0046 }
0047 
0048 //! Custom strcmpn() which works on different character types.
0049 /*! \tparam Ch Character type (e.g. char, wchar_t, short)
0050     \param s1 Null-terminated input string.
0051     \param s2 Null-terminated input string.
0052     \return 0 if equal
0053 */
0054 template<typename Ch>
0055 inline int StrCmp(const Ch* s1, const Ch* s2) {
0056     RAPIDJSON_ASSERT(s1 != 0);
0057     RAPIDJSON_ASSERT(s2 != 0);
0058     while(*s1 && (*s1 == *s2)) { s1++; s2++; }
0059     return static_cast<unsigned>(*s1) < static_cast<unsigned>(*s2) ? -1 : static_cast<unsigned>(*s1) > static_cast<unsigned>(*s2);
0060 }
0061 
0062 //! Returns number of code points in a encoded string.
0063 template<typename Encoding>
0064 bool CountStringCodePoint(const typename Encoding::Ch* s, SizeType length, SizeType* outCount) {
0065     RAPIDJSON_ASSERT(s != 0);
0066     RAPIDJSON_ASSERT(outCount != 0);
0067     GenericStringStream<Encoding> is(s);
0068     const typename Encoding::Ch* end = s + length;
0069     SizeType count = 0;
0070     while (is.src_ < end) {
0071         unsigned codepoint;
0072         if (!Encoding::Decode(is, &codepoint))
0073             return false;
0074         count++;
0075     }
0076     *outCount = count;
0077     return true;
0078 }
0079 
0080 } // namespace internal
0081 RAPIDJSON_NAMESPACE_END
0082 
0083 #endif // RAPIDJSON_INTERNAL_STRFUNC_H_