strfunc.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #ifndef RAPIDJSON_INTERNAL_STRFUNC_H_
  15. #define RAPIDJSON_INTERNAL_STRFUNC_H_
  16. #include "../stream.h"
  17. #include <cwchar>
  18. RAPIDJSON_NAMESPACE_BEGIN
  19. namespace internal {
  20. //! Custom strlen() which works on different character types.
  21. /*! \tparam Ch Character type (e.g. char, wchar_t, short)
  22. \param s Null-terminated input string.
  23. \return Number of characters in the string.
  24. \note This has the same semantics as strlen(), the return value is not number of Unicode codepoints.
  25. */
  26. template <typename Ch>
  27. inline SizeType StrLen(const Ch* s) {
  28. RAPIDJSON_ASSERT(s != 0);
  29. const Ch* p = s;
  30. while (*p) ++p;
  31. return SizeType(p - s);
  32. }
  33. template <>
  34. inline SizeType StrLen(const char* s) {
  35. return SizeType(std::strlen(s));
  36. }
  37. template <>
  38. inline SizeType StrLen(const wchar_t* s) {
  39. return SizeType(std::wcslen(s));
  40. }
  41. //! Custom strcmpn() which works on different character types.
  42. /*! \tparam Ch Character type (e.g. char, wchar_t, short)
  43. \param s1 Null-terminated input string.
  44. \param s2 Null-terminated input string.
  45. \return 0 if equal
  46. */
  47. template<typename Ch>
  48. inline int StrCmp(const Ch* s1, const Ch* s2) {
  49. RAPIDJSON_ASSERT(s1 != 0);
  50. RAPIDJSON_ASSERT(s2 != 0);
  51. while(*s1 && (*s1 == *s2)) { s1++; s2++; }
  52. return static_cast<unsigned>(*s1) < static_cast<unsigned>(*s2) ? -1 : static_cast<unsigned>(*s1) > static_cast<unsigned>(*s2);
  53. }
  54. //! Returns number of code points in a encoded string.
  55. template<typename Encoding>
  56. bool CountStringCodePoint(const typename Encoding::Ch* s, SizeType length, SizeType* outCount) {
  57. RAPIDJSON_ASSERT(s != 0);
  58. RAPIDJSON_ASSERT(outCount != 0);
  59. GenericStringStream<Encoding> is(s);
  60. const typename Encoding::Ch* end = s + length;
  61. SizeType count = 0;
  62. while (is.src_ < end) {
  63. unsigned codepoint;
  64. if (!Encoding::Decode(is, &codepoint))
  65. return false;
  66. count++;
  67. }
  68. *outCount = count;
  69. return true;
  70. }
  71. } // namespace internal
  72. RAPIDJSON_NAMESPACE_END
  73. #endif // RAPIDJSON_INTERNAL_STRFUNC_H_