Skip to content

Commit

Permalink
Merge pull request #872 from StilesCrisis/issue845_native_strlen
Browse files Browse the repository at this point in the history
Use native strlen
  • Loading branch information
miloyip committed Mar 6, 2017
2 parents 02de698 + c4e3d62 commit a1fac15
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 9 deletions.
11 changes: 11 additions & 0 deletions include/rapidjson/internal/strfunc.h
Expand Up @@ -16,6 +16,7 @@
#define RAPIDJSON_INTERNAL_STRFUNC_H_

#include "../stream.h"
#include <cwchar>

RAPIDJSON_NAMESPACE_BEGIN
namespace internal {
Expand All @@ -34,6 +35,16 @@ inline SizeType StrLen(const Ch* 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.
template<typename Encoding>
bool CountStringCodePoint(const typename Encoding::Ch* s, SizeType length, SizeType* outCount) {
Expand Down
24 changes: 20 additions & 4 deletions include/rapidjson/prettywriter.h
Expand Up @@ -107,7 +107,8 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
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);
(void)copy;
PrettyPrefix(kStringType);
Expand All @@ -126,7 +127,8 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
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
bool Key(const std::basic_string<Ch>& str) {
Expand Down Expand Up @@ -184,8 +186,22 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
//@{

//! Simpler but slower overload.
bool String(const Ch* str) { return String(str, internal::StrLen(str)); }
bool Key(const Ch* str) { return Key(str, internal::StrLen(str)); }
template <typename T>
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);
}

//@}

Expand Down
25 changes: 21 additions & 4 deletions include/rapidjson/writer.h
Expand Up @@ -16,6 +16,7 @@
#define RAPIDJSON_WRITER_H_

#include "stream.h"
#include "internal/meta.h"
#include "internal/stack.h"
#include "internal/strfunc.h"
#include "internal/dtoa.h"
Expand Down Expand Up @@ -198,7 +199,8 @@ class Writer {
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);
(void)copy;
Prefix(kStringType);
Expand All @@ -217,7 +219,8 @@ class Writer {
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) {
(void)memberCount;
Expand Down Expand Up @@ -247,8 +250,22 @@ class Writer {
//@{

//! Simpler but slower overload.
bool String(const Ch* str) { return String(str, internal::StrLen(str)); }
bool Key(const Ch* str) { return Key(str, internal::StrLen(str)); }
template <typename T>
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);
}

//@}

Expand Down
2 changes: 1 addition & 1 deletion test/unittest/CMakeLists.txt
Expand Up @@ -79,7 +79,7 @@ add_test(NAME unittest
if(NOT MSVC)
# Not running SIMD.* unit test cases for Valgrind
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)

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
Expand Down
17 changes: 17 additions & 0 deletions test/valgrind.supp
@@ -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
}

0 comments on commit a1fac15

Please sign in to comment.