DeepCover Embedded Security in IoT: Public-key Secured Data Paths

Dependencies:   MaximInterface

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers strfunc.h Source File

strfunc.h

00001 // Tencent is pleased to support the open source community by making RapidJSON available.
00002 // 
00003 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
00004 //
00005 // Licensed under the MIT License (the "License"); you may not use this file except
00006 // in compliance with the License. You may obtain a copy of the License at
00007 //
00008 // http://opensource.org/licenses/MIT
00009 //
00010 // Unless required by applicable law or agreed to in writing, software distributed 
00011 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
00012 // CONDITIONS OF ANY KIND, either express or implied. See the License for the 
00013 // specific language governing permissions and limitations under the License.
00014 
00015 #ifndef RAPIDJSON_INTERNAL_STRFUNC_H_
00016 #define RAPIDJSON_INTERNAL_STRFUNC_H_
00017 
00018 #include "../stream.h"
00019 
00020 RAPIDJSON_NAMESPACE_BEGIN
00021 namespace internal {
00022 
00023 //! Custom strlen() which works on different character types.
00024 /*! \tparam Ch Character type (e.g. char, wchar_t, short)
00025     \param s Null-terminated input string.
00026     \return Number of characters in the string. 
00027     \note This has the same semantics as strlen(), the return value is not number of Unicode codepoints.
00028 */
00029 template <typename Ch>
00030 inline SizeType StrLen(const Ch* s) {
00031     RAPIDJSON_ASSERT(s != 0);
00032     const Ch* p = s;
00033     while (*p) ++p;
00034     return SizeType(p - s);
00035 }
00036 
00037 //! Returns number of code points in a encoded string.
00038 template<typename Encoding>
00039 bool CountStringCodePoint(const typename Encoding::Ch* s, SizeType length, SizeType* outCount) {
00040     RAPIDJSON_ASSERT(s != 0);
00041     RAPIDJSON_ASSERT(outCount != 0);
00042     GenericStringStream<Encoding> is(s);
00043     const typename Encoding::Ch* end = s + length;
00044     SizeType count = 0;
00045     while (is.src_ < end) {
00046         unsigned codepoint;
00047         if (!Encoding::Decode(is, &codepoint))
00048             return false;
00049         count++;
00050     }
00051     *outCount = count;
00052     return true;
00053 }
00054 
00055 } // namespace internal
00056 RAPIDJSON_NAMESPACE_END
00057 
00058 #endif // RAPIDJSON_INTERNAL_STRFUNC_H_