Coverage Report

Created: 2026-09-01 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/arduinojson/src/ArduinoJson/Strings/JsonString.hpp
Line
Count
Source
1
// ArduinoJson - https://arduinojson.org
2
// Copyright © 2014-2026, Benoit BLANCHON
3
// MIT License
4
5
#pragma once
6
7
#include <ArduinoJson/Polyfills/type_traits.hpp>
8
#include <ArduinoJson/Strings/Adapters/RamString.hpp>
9
10
#if ARDUINOJSON_ENABLE_STD_STREAM
11
#  include <ostream>
12
#endif
13
14
ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE
15
16
// A string.
17
// https://arduinojson.org/v7/api/jsonstring/
18
class JsonString {
19
  friend struct detail::StringAdapter<JsonString>;
20
21
 public:
22
0
  JsonString() : str_(nullptr, 0) {}
23
24
7.21k
  JsonString(const char* data) : str_(data, data ? ::strlen(data) : 0) {}
25
26
  ARDUINOJSON_DEPRECATED(
27
      "ArduinoJson doesn't differentiate between static and dynamic strings "
28
      "anymore. Remove the second argument to fix this warning.")
29
0
  JsonString(const char* data, bool) : JsonString(data) {}
30
31
  template <typename TSize,
32
            detail::enable_if_t<detail::is_integral<TSize>::value &&
33
                                    !detail::is_same<TSize, bool>::value,
34
                                int> = 0>
35
11.0k
  JsonString(const char* data, TSize sz) : str_(data, size_t(sz)) {}
_ZN11ArduinoJson8V742HB4210JsonStringC2ItTnNS0_6detail9enable_ifIXaasr6detail11is_integralIT_EE5valuentsr6detail7is_sameIS5_bEE5valueEiE4typeELi0EEEPKcS5_
Line
Count
Source
35
4.79k
  JsonString(const char* data, TSize sz) : str_(data, size_t(sz)) {}
_ZN11ArduinoJson8V742HB4210JsonStringC2ImTnNS0_6detail9enable_ifIXaasr6detail11is_integralIT_EE5valuentsr6detail7is_sameIS5_bEE5valueEiE4typeELi0EEEPKcS5_
Line
Count
Source
35
6.25k
  JsonString(const char* data, TSize sz) : str_(data, size_t(sz)) {}
36
37
  ARDUINOJSON_DEPRECATED(
38
      "ArduinoJson doesn't differentiate between static and dynamic strings "
39
      "anymore. Remove the third argument to fix this warning.")
40
0
  JsonString(const char* data, size_t sz, bool) : JsonString(data, sz) {}
41
42
  // Returns a pointer to the characters.
43
1.11k
  const char* c_str() const {
44
1.11k
    return str_.data();
45
1.11k
  }
46
47
  // Returns true if the string is null.
48
0
  bool isNull() const {
49
0
    return str_.isNull();
50
0
  }
51
52
  // Deprecated: always returns false.
53
  ARDUINOJSON_DEPRECATED("The isStatic() was removed in v7.5")
54
0
  bool isStatic() const {
55
0
    return false;
56
0
  }
57
58
  // Returns length of the string.
59
1.11k
  size_t size() const {
60
1.11k
    return str_.size();
61
1.11k
  }
62
63
  // Returns true if the string is non-null
64
0
  explicit operator bool() const {
65
0
    return str_.data() != 0;
66
0
  }
67
68
  // Returns true if strings are equal.
69
0
  friend bool operator==(JsonString lhs, JsonString rhs) {
70
0
    if (lhs.size() != rhs.size())
71
0
      return false;
72
0
    if (lhs.c_str() == rhs.c_str())
73
0
      return true;
74
0
    if (!lhs.c_str())
75
0
      return false;
76
0
    if (!rhs.c_str())
77
0
      return false;
78
0
    return memcmp(lhs.c_str(), rhs.c_str(), lhs.size()) == 0;
79
0
  }
80
81
  // Returns true if strings differs.
82
0
  friend bool operator!=(JsonString lhs, JsonString rhs) {
83
0
    return !(lhs == rhs);
84
0
  }
85
86
#if ARDUINOJSON_ENABLE_STD_STREAM
87
0
  friend std::ostream& operator<<(std::ostream& lhs, const JsonString& rhs) {
88
0
    lhs.write(rhs.c_str(), static_cast<std::streamsize>(rhs.size()));
89
0
    return lhs;
90
0
  }
91
#endif
92
93
 private:
94
  detail::RamString str_;
95
};
96
97
namespace detail {
98
template <>
99
struct StringAdapter<JsonString> {
100
  using AdaptedString = RamString;
101
102
17.1k
  static const AdaptedString& adapt(const JsonString& s) {
103
17.1k
    return s.str_;
104
17.1k
  }
105
};
106
}  // namespace detail
107
108
ARDUINOJSON_END_PUBLIC_NAMESPACE