/src/arduinojson/src/ArduinoJson/Strings/StringAdapters.hpp
Line | Count | Source |
1 | | // ArduinoJson - https://arduinojson.org |
2 | | // Copyright © 2014-2025, 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 | | #include <ArduinoJson/Strings/Adapters/StringObject.hpp> |
10 | | |
11 | | #if ARDUINOJSON_ENABLE_PROGMEM |
12 | | # include <ArduinoJson/Strings/Adapters/FlashString.hpp> |
13 | | #endif |
14 | | |
15 | | ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE |
16 | | |
17 | | template <typename TAdaptedString1, typename TAdaptedString2> |
18 | | enable_if_t<TAdaptedString1::typeSortKey <= TAdaptedString2::typeSortKey, int> |
19 | 0 | stringCompare(TAdaptedString1 s1, TAdaptedString2 s2) { |
20 | 0 | ARDUINOJSON_ASSERT(!s1.isNull()); |
21 | 0 | ARDUINOJSON_ASSERT(!s2.isNull()); |
22 | 0 | size_t size1 = s1.size(); |
23 | 0 | size_t size2 = s2.size(); |
24 | 0 | size_t n = size1 < size2 ? size1 : size2; |
25 | 0 | for (size_t i = 0; i < n; i++) { |
26 | 0 | if (s1[i] != s2[i]) |
27 | 0 | return s1[i] - s2[i]; |
28 | 0 | } |
29 | 0 | if (size1 < size2) |
30 | 0 | return -1; |
31 | 0 | if (size1 > size2) |
32 | 0 | return 1; |
33 | 0 | return 0; |
34 | 0 | } |
35 | | |
36 | | template <typename TAdaptedString1, typename TAdaptedString2> |
37 | | enable_if_t<(TAdaptedString1::typeSortKey > TAdaptedString2::typeSortKey), int> |
38 | | stringCompare(TAdaptedString1 s1, TAdaptedString2 s2) { |
39 | | return -stringCompare(s2, s1); |
40 | | } |
41 | | |
42 | | template <typename TAdaptedString1, typename TAdaptedString2> |
43 | | enable_if_t<TAdaptedString1::typeSortKey <= TAdaptedString2::typeSortKey, bool> |
44 | 8.96k | stringEquals(TAdaptedString1 s1, TAdaptedString2 s2) { |
45 | 8.96k | ARDUINOJSON_ASSERT(!s1.isNull()); |
46 | 8.96k | ARDUINOJSON_ASSERT(!s2.isNull()); |
47 | 8.96k | size_t size1 = s1.size(); |
48 | 8.96k | size_t size2 = s2.size(); |
49 | 8.96k | if (size1 != size2) |
50 | 636 | return false; |
51 | 12.3k | for (size_t i = 0; i < size1; i++) { |
52 | 11.5k | if (s1[i] != s2[i]) |
53 | 7.55k | return false; |
54 | 11.5k | } |
55 | 776 | return true; |
56 | 8.32k | } |
57 | | |
58 | | template <typename TAdaptedString1, typename TAdaptedString2> |
59 | | enable_if_t<(TAdaptedString1::typeSortKey > TAdaptedString2::typeSortKey), bool> |
60 | | stringEquals(TAdaptedString1 s1, TAdaptedString2 s2) { |
61 | | return stringEquals(s2, s1); |
62 | | } |
63 | | |
64 | | template <typename TAdaptedString> |
65 | 0 | static void stringGetChars(TAdaptedString s, char* p, size_t n) { |
66 | 0 | ARDUINOJSON_ASSERT(s.size() <= n); |
67 | 0 | for (size_t i = 0; i < n; i++) { |
68 | 0 | p[i] = s[i]; |
69 | 0 | } |
70 | 0 | } |
71 | | |
72 | | ARDUINOJSON_END_PRIVATE_NAMESPACE |