Skip to content

Commit a90e86e

Browse files
committed
64 bit integer to string conversion optimization
1 parent ec93413 commit a90e86e

File tree

8 files changed

+51
-76
lines changed

8 files changed

+51
-76
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/mobizt/FirebaseClient/.github%2Fworkflows%2Fcompile_library.yml?logo=github&label=compile) [![Github Stars](https://img.shields.io/github/stars/mobizt/FirebaseClient?logo=github)](https://github.com/mobizt/FirebaseClient/stargazers) ![Github Issues](https://img.shields.io/github/issues/mobizt/FirebaseClient?logo=github)
44

5-
![GitHub Release](https://img.shields.io/github/v/release/mobizt/FirebaseClient) ![Arduino](https://img.shields.io/badge/Arduino-v1.4.2-57C207?logo=arduino) ![PlatformIO](https://badges.registry.platformio.org/packages/mobizt/library/FirebaseClient.svg) ![GitHub Release Date](https://img.shields.io/github/release-date/mobizt/FirebaseClient)
5+
![GitHub Release](https://img.shields.io/github/v/release/mobizt/FirebaseClient) ![Arduino](https://img.shields.io/badge/Arduino-v1.4.3-57C207?logo=arduino) ![PlatformIO](https://badges.registry.platformio.org/packages/mobizt/library/FirebaseClient.svg) ![GitHub Release Date](https://img.shields.io/github/release-date/mobizt/FirebaseClient)
66

77
[![GitHub Sponsors](https://img.shields.io/github/sponsors/mobizt?logo=github)](https://github.com/sponsors/mobizt)
88

9-
Revision `2024-10-25T05:11:56Z`
9+
Revision `2024-10-29T02:35:03Z`
1010

1111
## Table of Contents
1212

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "FirebaseClient",
3-
"version": "1.4.2",
3+
"version": "1.4.3",
44
"keywords": "communication, REST, esp32, esp8266, arduino",
55
"description": "Async Firebase Client library for Arduino.",
66
"repository": {

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name=FirebaseClient
22

3-
version=1.4.2
3+
version=1.4.3
44

55
author=Mobizt
66

src/core/AsyncResult/Value.h

Lines changed: 5 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Created October 25, 2024
2+
* Created October 29, 2024
33
*
44
* The MIT License (MIT)
55
* Copyright (c) 2024 K. Suwatchai (Mobizt)
@@ -40,65 +40,6 @@ enum realtime_database_data_type
4040
realtime_database_data_type_array = 7
4141
};
4242

43-
class NumToString
44-
{
45-
public:
46-
NumToString() {}
47-
template <typename T = uint64_t>
48-
auto val(T value, bool neg = false) -> typename std::enable_if<(std::is_same<T, uint64_t>::value), String>::type
49-
{
50-
uint8_t i = 21;
51-
char buff[23] = {0};
52-
53-
if (value == 0)
54-
buff[i--] = '0';
55-
else
56-
{
57-
uint16_t r = 0;
58-
while (value > 10000)
59-
{
60-
uint64_t q = value / 10000;
61-
r = value - (q * 10000);
62-
63-
for (uint8_t j = 0; j < 4; j++)
64-
{
65-
uint16_t rq = r / 10;
66-
buff[i--] = r - (rq * 10) + '0';
67-
r = rq;
68-
}
69-
70-
value = q;
71-
}
72-
73-
r = value;
74-
while (r > 0)
75-
{
76-
uint16_t q = r / 10;
77-
buff[i--] = r - (q * 10) + '0';
78-
r = q;
79-
}
80-
}
81-
82-
if (neg)
83-
buff[i--] = '-';
84-
85-
return String(&buff[i + 1]);
86-
}
87-
88-
template <typename T = int64_t>
89-
auto val(T value) -> typename std::enable_if<(std::is_same<T, int64_t>::value), String>::type
90-
{
91-
uint64_t uvalue = value < 0 ? -value : value;
92-
return val(uvalue, value < 0);
93-
}
94-
95-
template <typename T = int>
96-
auto val(T value) -> typename std::enable_if<(!std::is_same<T, uint64_t>::value && !std::is_same<T, int64_t>::value), String>::type
97-
{
98-
return String(value);
99-
}
100-
};
101-
10243
struct boolean_t : public Printable
10344
{
10445
private:
@@ -120,14 +61,14 @@ struct number_t : public Printable
12061
{
12162
private:
12263
String buf;
123-
NumToString num2Str;
64+
StringUtil sut;
12465

12566
public:
12667
number_t() {}
12768
template <typename T1 = int, typename T = int>
12869
explicit number_t(T1 v, T d) : buf(String(v, d)) {}
12970
template <typename T = int>
130-
explicit number_t(T o) : buf(num2Str.val(o)) {}
71+
explicit number_t(T o) : buf(sut.num2Str(o)) {}
13172
const char *c_str() const { return buf.c_str(); }
13273
size_t printTo(Print &p) const override { return p.print(buf.c_str()); }
13374
};
@@ -289,8 +230,8 @@ class ValueConverter
289230
if (v_sring<T>::value)
290231
buf += '\"';
291232

292-
NumToString num2Str;
293-
buf += num2Str.val(value);
233+
StringUtil sut;
234+
buf += sut.num2Str(value);
294235

295236
if (v_sring<T>::value)
296237
buf += '\"';

src/core/Core.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#undef FIREBASE_CLIENT_VERSION
88
#endif
99

10-
#define FIREBASE_CLIENT_VERSION "1.4.2"
10+
#define FIREBASE_CLIENT_VERSION "1.4.3"
1111

1212
static void sys_idle()
1313
{

src/core/ObjectWriter.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Created June 12, 2024
2+
* Created October 29, 2024
33
*
44
* The MIT License (MIT)
55
* Copyright (c) 2024 K. Suwatchai (Mobizt)
@@ -173,6 +173,7 @@ class BufWriter
173173
private:
174174
ObjectWriter owriter;
175175
JSONUtil jut;
176+
StringUtil sut;
176177

177178
template <typename T>
178179
struct v_number
@@ -207,7 +208,7 @@ class BufWriter
207208
auto add(T1 ret, const T2 &value, String &buf, const String &name) -> typename std::enable_if<v_number<T2>::value, T1>::type
208209
{
209210
clear(buf);
210-
jut.addObject(buf, name, String(value), false, true);
211+
jut.addObject(buf, name, sut.num2Str(value), false, true);
211212
return ret;
212213
}
213214

@@ -237,7 +238,7 @@ class BufWriter
237238
template <typename T1, typename T2>
238239
auto set(T1 ret, const T2 &value, String *buf, size_t bufSize, uint8_t index, const String &name) -> typename std::enable_if<v_number<T2>::value, T1>::type
239240
{
240-
setObject(buf, bufSize, index, name, String(value), false, true);
241+
setObject(buf, bufSize, index, name, sut.num2Str(value), false, true);
241242
return ret;
242243
}
243244

@@ -265,7 +266,7 @@ class BufWriter
265266
template <typename T1, typename T2>
266267
auto append(T1 ret, const T2 &value, String *buf, size_t bufSize, uint8_t index, const String &name) -> typename std::enable_if<v_number<T2>::value, T1>::type
267268
{
268-
owriter.addMapArrayMember(buf, bufSize, index, name, String(value), false);
269+
owriter.addMapArrayMember(buf, bufSize, index, name, sut.num2Str(value), false);
269270
return ret;
270271
}
271272

src/core/StringUtil.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,39 @@ class StringUtil
113113
v = ndx;
114114
return v;
115115
}
116+
117+
// Some cores do not provide 64-bit integer to string conversion.
118+
template <typename T = uint64_t>
119+
auto num2Str(T val, bool negative = false) -> typename std::enable_if<(std::is_same<T, uint64_t>::value), String>::type
120+
{
121+
String v;
122+
char buffer[21];
123+
char *ndx = &buffer[sizeof(buffer) - 1];
124+
*ndx = '\0';
125+
do
126+
{
127+
*--ndx = val % 10 + '0';
128+
val = val / 10;
129+
} while (val != 0);
130+
131+
if (negative)
132+
v += '-';
133+
v += ndx;
134+
return v;
135+
}
136+
137+
template <typename T = int64_t>
138+
auto num2Str(T val) -> typename std::enable_if<(std::is_same<T, int64_t>::value), String>::type
139+
{
140+
uint64_t value = val < 0 ? -val : val;
141+
return num2Str(value, val < 0);
142+
}
143+
144+
template <typename T = int>
145+
auto num2Str(T val) -> typename std::enable_if<(!std::is_same<T, uint64_t>::value && !std::is_same<T, int64_t>::value), String>::type
146+
{
147+
return String(val);
148+
}
116149
};
117150

118151
#endif

src/firestore/Values.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Created October 25, 2024
2+
* Created October 29, 2024
33
*
44
* The MIT License (MIT)
55
* Copyright (c) 2024 K. Suwatchai (Mobizt)
@@ -153,15 +153,15 @@ namespace Values
153153
private:
154154
String buf, str;
155155
ObjectWriter owriter;
156-
NumToString num2Str;
156+
StringUtil sut;
157157
const char *getVal() { return owriter.setPair(str, firestore_const_key[firestore_const_key_integerValue].text, buf); }
158158

159159
public:
160160
/**
161161
* A 64-bit signed integer value.
162162
* @param value The 64-bit signed integer value.
163163
*/
164-
explicit IntegerValue(int64_t value) : buf(StringValue(num2Str.val(value)).c_str()) { getVal(); }
164+
explicit IntegerValue(int64_t value) : buf(StringValue(sut.num2Str(value)).c_str()) { getVal(); }
165165
const char *c_str() const { return buf.c_str(); }
166166
const char *val() { return getVal(); }
167167
size_t printTo(Print &p) const override { return p.print(str.c_str()); }

0 commit comments

Comments
 (0)