• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

bblanchon / ArduinoJson / 13563980954

27 Feb 2025 10:13AM CUT coverage: 99.325%. First build
13563980954

push

github

bblanchon
Reduce code size

13 of 13 new or added lines in 1 file covered. (100.0%)

3974 of 4001 relevant lines covered (99.33%)

10896.33 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.55
/src/ArduinoJson/Polyfills/pgmspace.hpp
1
// ArduinoJson - https://arduinojson.org
2
// Copyright © 2014-2025, Benoit BLANCHON
3
// MIT License
4

5
#pragma once
6

7
#ifdef ARDUINO
8
#  include <Arduino.h>
9
#else
10
// Allow using PROGMEM outside of Arduino (issue #1903)
11
class __FlashStringHelper;
12
#  include <avr/pgmspace.h>
13
#endif
14

15
#include <ArduinoJson/Configuration.hpp>
16
#include <ArduinoJson/Namespace.hpp>
17
#include <ArduinoJson/Polyfills/assert.hpp>
18

19
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
20
// Wraps a const char* so that the our functions are picked only if the
21
// originals are missing
22
struct pgm_p {
23
  pgm_p(const void* p) : address(reinterpret_cast<const char*>(p)) {}
90✔
24
  const char* address;
25
};
26

27
ARDUINOJSON_END_PRIVATE_NAMESPACE
28

29
#ifndef strlen_P
30
inline size_t strlen_P(ArduinoJson::detail::pgm_p s) {
30✔
31
  const char* p = s.address;
30✔
32
  ARDUINOJSON_ASSERT(p != NULL);
33
  while (pgm_read_byte(p))
180✔
34
    p++;
150✔
35
  return size_t(p - s.address);
30✔
36
}
37
#endif
38

39
#ifndef strncmp_P
40
inline int strncmp_P(const char* a, ArduinoJson::detail::pgm_p b, size_t n) {
7✔
41
  const char* s1 = a;
7✔
42
  const char* s2 = b.address;
7✔
43
  ARDUINOJSON_ASSERT(s1 != NULL);
44
  ARDUINOJSON_ASSERT(s2 != NULL);
45
  while (n-- > 0) {
8✔
46
    char c1 = *s1++;
6✔
47
    char c2 = static_cast<char>(pgm_read_byte(s2++));
6✔
48
    if (c1 < c2)
6✔
49
      return -1;
2✔
50
    if (c1 > c2)
4✔
51
      return 1;
2✔
52
    if (c1 == 0 /* and c2 as well */)
2✔
53
      return 0;
1✔
54
  }
55
  return 0;
2✔
56
}
57
#endif
58

59
#ifndef strcmp_P
60
inline int strcmp_P(const char* a, ArduinoJson::detail::pgm_p b) {
6✔
61
  const char* s1 = a;
6✔
62
  const char* s2 = b.address;
6✔
63
  ARDUINOJSON_ASSERT(s1 != NULL);
64
  ARDUINOJSON_ASSERT(s2 != NULL);
65
  for (;;) {
66
    char c1 = *s1++;
11✔
67
    char c2 = static_cast<char>(pgm_read_byte(s2++));
11✔
68
    if (c1 < c2)
11✔
69
      return -1;
2✔
70
    if (c1 > c2)
9✔
71
      return 1;
2✔
72
    if (c1 == 0 /* and c2 as well */)
7✔
73
      return 0;
2✔
74
  }
5✔
75
}
76
#endif
77

78
#ifndef memcmp_P
79
inline int memcmp_P(const void* a, ArduinoJson::detail::pgm_p b, size_t n) {
17✔
80
  const uint8_t* p1 = reinterpret_cast<const uint8_t*>(a);
17✔
81
  const char* p2 = b.address;
17✔
82
  ARDUINOJSON_ASSERT(p1 != NULL);
83
  ARDUINOJSON_ASSERT(p2 != NULL);
84
  while (n-- > 0) {
79✔
85
    uint8_t v1 = *p1++;
67✔
86
    uint8_t v2 = pgm_read_byte(p2++);
67✔
87
    if (v1 != v2)
67✔
88
      return v1 - v2;
5✔
89
  }
90
  return 0;
12✔
91
}
92
#endif
93

94
#ifndef memcpy_P
95
inline void* memcpy_P(void* dst, ArduinoJson::detail::pgm_p src, size_t n) {
22✔
96
  uint8_t* d = reinterpret_cast<uint8_t*>(dst);
22✔
97
  const char* s = src.address;
22✔
98
  ARDUINOJSON_ASSERT(d != NULL);
99
  ARDUINOJSON_ASSERT(s != NULL);
100
  while (n-- > 0) {
141✔
101
    *d++ = pgm_read_byte(s++);
119✔
102
  }
103
  return dst;
22✔
104
}
105
#endif
106

107
#ifndef pgm_read_dword
108
inline uint32_t pgm_read_dword(ArduinoJson::detail::pgm_p p) {
109
  uint32_t result;
110
  memcpy_P(&result, p.address, 4);
111
  return result;
112
}
113
#endif
114

115
#ifndef pgm_read_float
116
inline float pgm_read_float(ArduinoJson::detail::pgm_p p) {
2✔
117
  float result;
118
  memcpy_P(&result, p.address, sizeof(float));
2✔
119
  return result;
2✔
120
}
121
#endif
122

123
#ifndef pgm_read_double
124
#  if defined(__SIZEOF_DOUBLE__) && defined(__SIZEOF_FLOAT__) && \
125
      __SIZEOF_DOUBLE__ == __SIZEOF_FLOAT__
126
inline double pgm_read_double(ArduinoJson::detail::pgm_p p) {
127
  return pgm_read_float(p.address);
128
}
129
#  else
130
inline double pgm_read_double(ArduinoJson::detail::pgm_p p) {
×
131
  double result;
132
  memcpy_P(&result, p.address, sizeof(double));
×
133
  return result;
×
134
}
135
#  endif
136
#endif
137

138
#ifndef pgm_read_ptr
139
inline void* pgm_read_ptr(ArduinoJson::detail::pgm_p p) {
6✔
140
  void* result;
141
  memcpy_P(&result, p.address, sizeof(result));
6✔
142
  return result;
6✔
143
}
144
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc