Coverage Report

Created: 2024-09-08 06:19

/src/arduinojson/src/ArduinoJson/Strings/JsonString.hpp
Line
Count
Source (jump to first uncovered line)
1
// ArduinoJson - https://arduinojson.org
2
// Copyright © 2014-2024, Benoit BLANCHON
3
// MIT License
4
5
#pragma once
6
7
#if ARDUINOJSON_ENABLE_STD_STREAM
8
#  include <ostream>
9
#endif
10
11
ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE
12
13
// A string.
14
// https://arduinojson.org/v7/api/jsonstring/
15
class JsonString {
16
 public:
17
  enum Ownership { Copied, Linked };
18
19
0
  JsonString() : data_(0), size_(0), ownership_(Linked) {}
20
21
  JsonString(const char* data, Ownership ownership = Linked)
22
1.61k
      : data_(data), size_(data ? ::strlen(data) : 0), ownership_(ownership) {}
23
24
  JsonString(const char* data, size_t sz, Ownership ownership = Linked)
25
3.72k
      : data_(data), size_(sz), ownership_(ownership) {}
26
27
  // Returns a pointer to the characters.
28
3.72k
  const char* c_str() const {
29
3.72k
    return data_;
30
3.72k
  }
31
32
  // Returns true if the string is null.
33
0
  bool isNull() const {
34
0
    return !data_;
35
0
  }
36
37
  // Returns true if the string is stored by address.
38
  // Returns false if the string is stored by copy.
39
0
  bool isLinked() const {
40
0
    return ownership_ == Linked;
41
0
  }
42
43
  // Returns length of the string.
44
1.61k
  size_t size() const {
45
1.61k
    return size_;
46
1.61k
  }
47
48
  // Returns true if the string is non-null
49
0
  explicit operator bool() const {
50
0
    return data_ != 0;
51
0
  }
52
53
  // Returns true if strings are equal.
54
1.61k
  friend bool operator==(JsonString lhs, JsonString rhs) {
55
1.61k
    if (lhs.size_ != rhs.size_)
56
112
      return false;
57
1.50k
    if (lhs.data_ == rhs.data_)
58
0
      return true;
59
1.50k
    if (!lhs.data_)
60
0
      return false;
61
1.50k
    if (!rhs.data_)
62
1.50k
      return false;
63
0
    return memcmp(lhs.data_, rhs.data_, lhs.size_) == 0;
64
1.50k
  }
65
66
  // Returns true if strings differs.
67
1.61k
  friend bool operator!=(JsonString lhs, JsonString rhs) {
68
1.61k
    return !(lhs == rhs);
69
1.61k
  }
70
71
#if ARDUINOJSON_ENABLE_STD_STREAM
72
0
  friend std::ostream& operator<<(std::ostream& lhs, const JsonString& rhs) {
73
0
    lhs.write(rhs.c_str(), static_cast<std::streamsize>(rhs.size()));
74
0
    return lhs;
75
0
  }
76
#endif
77
78
 private:
79
  const char* data_;
80
  size_t size_;
81
  Ownership ownership_;
82
};
83
84
ARDUINOJSON_END_PUBLIC_NAMESPACE