strfunc.h

Engine/source/persistence/rapidjson/internal/strfunc.h

More...

Namespaces:

namespace

Detailed Description

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