Merge pull request #872 from StilesCrisis/issue845_native_strlen
Use native strlen
This commit is contained in:
commit
a1fac159d7
@ -16,6 +16,7 @@
|
|||||||
#define RAPIDJSON_INTERNAL_STRFUNC_H_
|
#define RAPIDJSON_INTERNAL_STRFUNC_H_
|
||||||
|
|
||||||
#include "../stream.h"
|
#include "../stream.h"
|
||||||
|
#include <cwchar>
|
||||||
|
|
||||||
RAPIDJSON_NAMESPACE_BEGIN
|
RAPIDJSON_NAMESPACE_BEGIN
|
||||||
namespace internal {
|
namespace internal {
|
||||||
@ -34,6 +35,16 @@ inline SizeType StrLen(const Ch* s) {
|
|||||||
return SizeType(p - s);
|
return SizeType(p - s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
inline SizeType StrLen(const char* s) {
|
||||||
|
return SizeType(std::strlen(s));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
inline SizeType StrLen(const wchar_t* s) {
|
||||||
|
return SizeType(std::wcslen(s));
|
||||||
|
}
|
||||||
|
|
||||||
//! Returns number of code points in a encoded string.
|
//! Returns number of code points in a encoded string.
|
||||||
template<typename Encoding>
|
template<typename Encoding>
|
||||||
bool CountStringCodePoint(const typename Encoding::Ch* s, SizeType length, SizeType* outCount) {
|
bool CountStringCodePoint(const typename Encoding::Ch* s, SizeType length, SizeType* outCount) {
|
||||||
|
@ -107,7 +107,8 @@ public:
|
|||||||
return Base::WriteString(str, length);
|
return Base::WriteString(str, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool String(const Ch* str, SizeType length, bool copy = false) {
|
template <typename T>
|
||||||
|
RAPIDJSON_ENABLEIF_RETURN((internal::IsSame<Ch, T>), (bool)) String(const T* str, SizeType length, bool copy = false) {
|
||||||
RAPIDJSON_ASSERT(str != 0);
|
RAPIDJSON_ASSERT(str != 0);
|
||||||
(void)copy;
|
(void)copy;
|
||||||
PrettyPrefix(kStringType);
|
PrettyPrefix(kStringType);
|
||||||
@ -126,7 +127,8 @@ public:
|
|||||||
return Base::WriteStartObject();
|
return Base::WriteStartObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Key(const Ch* str, SizeType length, bool copy = false) { return String(str, length, copy); }
|
template <typename T>
|
||||||
|
RAPIDJSON_ENABLEIF_RETURN((internal::IsSame<Ch, T>), (bool)) Key(const T* str, SizeType length, bool copy = false) { return String(str, length, copy); }
|
||||||
|
|
||||||
#if RAPIDJSON_HAS_STDSTRING
|
#if RAPIDJSON_HAS_STDSTRING
|
||||||
bool Key(const std::basic_string<Ch>& str) {
|
bool Key(const std::basic_string<Ch>& str) {
|
||||||
@ -184,8 +186,22 @@ public:
|
|||||||
//@{
|
//@{
|
||||||
|
|
||||||
//! Simpler but slower overload.
|
//! Simpler but slower overload.
|
||||||
bool String(const Ch* str) { return String(str, internal::StrLen(str)); }
|
template <typename T>
|
||||||
bool Key(const Ch* str) { return Key(str, internal::StrLen(str)); }
|
RAPIDJSON_ENABLEIF_RETURN((internal::IsSame<Ch, T>), (bool)) String(const T* const& str) { return String(str, internal::StrLen(str)); }
|
||||||
|
template <typename T>
|
||||||
|
RAPIDJSON_ENABLEIF_RETURN((internal::IsSame<Ch, T>), (bool)) Key(const T* const& str) { return Key(str, internal::StrLen(str)); }
|
||||||
|
|
||||||
|
//! The compiler can give us the length of quoted strings for free.
|
||||||
|
template <typename T, size_t N>
|
||||||
|
RAPIDJSON_ENABLEIF_RETURN((internal::IsSame<Ch, T>), (bool)) String(const T (&str)[N]) {
|
||||||
|
RAPIDJSON_ASSERT(str[N-1] == '\0'); // you must pass in a null-terminated string (quoted constant strings are always null-terminated)
|
||||||
|
return String(str, N-1);
|
||||||
|
}
|
||||||
|
template <typename T, size_t N>
|
||||||
|
RAPIDJSON_ENABLEIF_RETURN((internal::IsSame<Ch, T>), (bool)) Key(const T (&str)[N]) {
|
||||||
|
RAPIDJSON_ASSERT(str[N-1] == '\0'); // you must pass in a null-terminated string (quoted constant strings are always null-terminated)
|
||||||
|
return Key(str, N-1);
|
||||||
|
}
|
||||||
|
|
||||||
//@}
|
//@}
|
||||||
|
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
#define RAPIDJSON_WRITER_H_
|
#define RAPIDJSON_WRITER_H_
|
||||||
|
|
||||||
#include "stream.h"
|
#include "stream.h"
|
||||||
|
#include "internal/meta.h"
|
||||||
#include "internal/stack.h"
|
#include "internal/stack.h"
|
||||||
#include "internal/strfunc.h"
|
#include "internal/strfunc.h"
|
||||||
#include "internal/dtoa.h"
|
#include "internal/dtoa.h"
|
||||||
@ -198,7 +199,8 @@ public:
|
|||||||
return EndValue(WriteString(str, length));
|
return EndValue(WriteString(str, length));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool String(const Ch* str, SizeType length, bool copy = false) {
|
template <typename T>
|
||||||
|
RAPIDJSON_ENABLEIF_RETURN((internal::IsSame<Ch, T>), (bool)) String(const T* str, SizeType length, bool copy = false) {
|
||||||
RAPIDJSON_ASSERT(str != 0);
|
RAPIDJSON_ASSERT(str != 0);
|
||||||
(void)copy;
|
(void)copy;
|
||||||
Prefix(kStringType);
|
Prefix(kStringType);
|
||||||
@ -217,7 +219,8 @@ public:
|
|||||||
return WriteStartObject();
|
return WriteStartObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Key(const Ch* str, SizeType length, bool copy = false) { return String(str, length, copy); }
|
template <typename T>
|
||||||
|
RAPIDJSON_ENABLEIF_RETURN((internal::IsSame<Ch, T>), (bool)) Key(const T* str, SizeType length, bool copy = false) { return String(str, length, copy); }
|
||||||
|
|
||||||
bool EndObject(SizeType memberCount = 0) {
|
bool EndObject(SizeType memberCount = 0) {
|
||||||
(void)memberCount;
|
(void)memberCount;
|
||||||
@ -247,8 +250,22 @@ public:
|
|||||||
//@{
|
//@{
|
||||||
|
|
||||||
//! Simpler but slower overload.
|
//! Simpler but slower overload.
|
||||||
bool String(const Ch* str) { return String(str, internal::StrLen(str)); }
|
template <typename T>
|
||||||
bool Key(const Ch* str) { return Key(str, internal::StrLen(str)); }
|
RAPIDJSON_ENABLEIF_RETURN((internal::IsSame<Ch, T>), (bool)) String(const T* const& str) { return String(str, internal::StrLen(str)); }
|
||||||
|
template <typename T>
|
||||||
|
RAPIDJSON_ENABLEIF_RETURN((internal::IsSame<Ch, T>), (bool)) Key(const T* const& str) { return Key(str, internal::StrLen(str)); }
|
||||||
|
|
||||||
|
//! The compiler can give us the length of quoted strings for free.
|
||||||
|
template <typename T, size_t N>
|
||||||
|
RAPIDJSON_ENABLEIF_RETURN((internal::IsSame<Ch, T>), (bool)) String(const T (&str)[N]) {
|
||||||
|
RAPIDJSON_ASSERT(str[N-1] == '\0'); // you must pass in a null-terminated string (quoted constant strings are always null-terminated)
|
||||||
|
return String(str, N-1);
|
||||||
|
}
|
||||||
|
template <typename T, size_t N>
|
||||||
|
RAPIDJSON_ENABLEIF_RETURN((internal::IsSame<Ch, T>), (bool)) Key(const T (&str)[N]) {
|
||||||
|
RAPIDJSON_ASSERT(str[N-1] == '\0'); // you must pass in a null-terminated string (quoted constant strings are always null-terminated)
|
||||||
|
return Key(str, N-1);
|
||||||
|
}
|
||||||
|
|
||||||
//@}
|
//@}
|
||||||
|
|
||||||
|
@ -79,7 +79,7 @@ add_test(NAME unittest
|
|||||||
if(NOT MSVC)
|
if(NOT MSVC)
|
||||||
# Not running SIMD.* unit test cases for Valgrind
|
# Not running SIMD.* unit test cases for Valgrind
|
||||||
add_test(NAME valgrind_unittest
|
add_test(NAME valgrind_unittest
|
||||||
COMMAND valgrind --leak-check=full --error-exitcode=1 ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/unittest --gtest_filter=-SIMD.*
|
COMMAND valgrind --suppressions=${CMAKE_SOURCE_DIR}/test/valgrind.supp --leak-check=full --error-exitcode=1 ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/unittest --gtest_filter=-SIMD.*
|
||||||
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
|
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
|
||||||
|
|
||||||
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||||
|
17
test/valgrind.supp
Normal file
17
test/valgrind.supp
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
Suppress wcslen valgrind report 1
|
||||||
|
Memcheck:Cond
|
||||||
|
fun:__wcslen_sse2
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
Suppress wcslen valgrind report 2
|
||||||
|
Memcheck:Addr8
|
||||||
|
fun:__wcslen_sse2
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
Suppress wcslen valgrind report 3
|
||||||
|
Memcheck:Value8
|
||||||
|
fun:__wcslen_sse2
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user