Skip to content

Commit dd7b013

Browse files
committed
size optimized string.cpp
1 parent 500d0be commit dd7b013

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed

src/libcxxrt/string.cpp

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
#include <stdexcept>
1515
#include <string>
1616

17+
#ifdef _EZ80
18+
#include <ti/sprintf.h>
19+
extern "C" int __strtoi(const char *__restrict nptr, char **__restrict endptr, int base) __attribute__((nonnull(1)));
20+
#endif // _EZ80
21+
1722
#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1823
# include <cwchar>
1924
#endif
@@ -60,10 +65,12 @@ template string operator+<char, char_traits<char>, allocator<char>>(char const*,
6065
namespace
6166
{
6267

68+
__attribute__((__unused__))
6369
inline void throw_from_string_out_of_range(const string& func) {
6470
std::__throw_out_of_range((func + ": out of range").c_str());
6571
}
6672

73+
__attribute__((__unused__))
6774
inline void throw_from_string_invalid_arg(const string& func) {
6875
std::__throw_invalid_argument((func + ": no conversion").c_str());
6976
}
@@ -91,6 +98,7 @@ template<typename V, typename S>
9198
inline V as_integer(const string& func, const S& s, size_t* idx, int base);
9299

93100
// string
101+
#ifndef _EZ80
94102
template<>
95103
inline int as_integer(const string& func, const string& s, size_t* idx, int base) {
96104
// Use long as no Standard string to integer exists.
@@ -119,6 +127,7 @@ template<>
119127
inline unsigned long long as_integer(const string& func, const string& s, size_t* idx, int base) {
120128
return as_integer_helper<unsigned long long>(func, s, idx, base, strtoull);
121129
}
130+
#endif // _EZ80
122131

123132
#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
124133
// wstring
@@ -177,6 +186,7 @@ inline V as_float_helper(const string& func, const S& str, size_t* idx, F f) {
177186
template<typename V, typename S>
178187
inline V as_float(const string& func, const S& s, size_t* idx = nullptr);
179188

189+
#ifndef _EZ80
180190
template<>
181191
inline float as_float(const string& func, const string& s, size_t* idx) {
182192
return as_float_helper<float>(func, s, idx, strtof);
@@ -191,6 +201,7 @@ template<>
191201
inline long double as_float(const string& func, const string& s, size_t* idx) {
192202
return as_float_helper<long double>(func, s, idx, strtold);
193203
}
204+
#endif // _EZ80
194205

195206
#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
196207
template<>
@@ -211,6 +222,8 @@ inline long double as_float(const string& func, const wstring& s, size_t* idx) {
211222

212223
} // unnamed namespace
213224

225+
#ifndef _EZ80
226+
214227
int stoi(const string& str, size_t* idx, int base) {
215228
return as_integer<int>("stoi", str, idx, base);
216229
}
@@ -243,6 +256,82 @@ long double stold(const string& str, size_t* idx) {
243256
return as_float<long double>("stold", str, idx);
244257
}
245258

259+
#else // _EZ80
260+
261+
int stoi(const string& str, size_t *pos, int base) {
262+
char *end_ptr;
263+
int result = static_cast<int>(__strtoi(str.c_str(), &end_ptr, base));
264+
if (pos != nullptr) {
265+
*pos = static_cast<size_t>(end_ptr - str.c_str());
266+
}
267+
return result;
268+
}
269+
270+
long stol(const string& str, size_t *pos, int base) {
271+
char *end_ptr;
272+
long result = std::strtol(str.c_str(), &end_ptr, base);
273+
if (pos != nullptr) {
274+
*pos = static_cast<size_t>(end_ptr - str.c_str());
275+
}
276+
return result;
277+
}
278+
279+
long long stoll(const string& str, size_t *pos, int base) {
280+
char *end_ptr;
281+
long long result = std::strtoll(str.c_str(), &end_ptr, base);
282+
if (pos != nullptr) {
283+
*pos = static_cast<size_t>(end_ptr - str.c_str());
284+
}
285+
return result;
286+
}
287+
288+
unsigned long stoul(const string& str, size_t *pos, int base) {
289+
char *end_ptr;
290+
unsigned long result = std::strtoul(str.c_str(), &end_ptr, base);
291+
if (pos != nullptr) {
292+
*pos = static_cast<size_t>(end_ptr - str.c_str());
293+
}
294+
return result;
295+
}
296+
297+
unsigned long long stoull(const string& str, size_t *pos, int base) {
298+
char *end_ptr;
299+
unsigned long long result = std::strtoull(str.c_str(), &end_ptr, base);
300+
if (pos != nullptr) {
301+
*pos = static_cast<size_t>(end_ptr - str.c_str());
302+
}
303+
return result;
304+
}
305+
306+
float stof(const string& str, size_t *pos) {
307+
char *end_ptr;
308+
float result = std::strtof(str.c_str(), &end_ptr);
309+
if (pos != nullptr) {
310+
*pos = static_cast<size_t>(end_ptr - str.c_str());
311+
}
312+
return result;
313+
}
314+
315+
double stod(const string& str, size_t *pos) {
316+
char *end_ptr;
317+
double result = std::strtod(str.c_str(), &end_ptr);
318+
if (pos != nullptr) {
319+
*pos = static_cast<size_t>(end_ptr - str.c_str());
320+
}
321+
return result;
322+
}
323+
324+
long double stold(const string& str, size_t *pos) {
325+
char *end_ptr;
326+
long double result = std::strtold(str.c_str(), &end_ptr);
327+
if (pos != nullptr) {
328+
*pos = static_cast<size_t>(end_ptr - str.c_str());
329+
}
330+
return result;
331+
}
332+
333+
#endif // _EZ80
334+
246335
#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
247336
int stoi(const wstring& str, size_t* idx, int base) {
248337
return as_integer<int>("stoi", str, idx, base);
@@ -352,13 +441,67 @@ S i_to_string(V v) {
352441

353442
} // unnamed namespace
354443

444+
#ifndef _EZ80
445+
355446
string to_string (int val) { return i_to_string< string>(val); }
356447
string to_string (long val) { return i_to_string< string>(val); }
357448
string to_string (long long val) { return i_to_string< string>(val); }
358449
string to_string (unsigned val) { return i_to_string< string>(val); }
359450
string to_string (unsigned long val) { return i_to_string< string>(val); }
360451
string to_string (unsigned long long val) { return i_to_string< string>(val); }
361452

453+
#else // _EZ80
454+
455+
string to_string(int value) {
456+
char buf[sizeof("-8388608")];
457+
boot_sprintf(buf, "%d", value);
458+
return string(buf);
459+
}
460+
461+
string to_string(unsigned int value) {
462+
char buf[sizeof("16777215")];
463+
boot_sprintf(buf, "%u", value);
464+
return string(buf);
465+
}
466+
467+
string to_string(long value) {
468+
char buf[sizeof("-2147483648")];
469+
std::sprintf(buf, "%ld", value);
470+
return string(buf);
471+
}
472+
473+
string to_string(unsigned long value) {
474+
char buf[sizeof("4294967295")];
475+
std::sprintf(buf, "%lu", value);
476+
return string(buf);
477+
}
478+
479+
string to_string(signed __int48 value) {
480+
char buf[sizeof("-140737488355328")];
481+
std::sprintf(buf, "%lld", static_cast<long long>(value));
482+
return string(buf);
483+
}
484+
485+
string to_string(unsigned __int48 value) {
486+
char buf[sizeof("281474976710655")];
487+
std::sprintf(buf, "%llu", static_cast<unsigned long long>(value));
488+
return string(buf);
489+
}
490+
491+
string to_string(long long value) {
492+
char buf[sizeof("-9223372036854775808")];
493+
std::sprintf(buf, "%lld", value);
494+
return string(buf);
495+
}
496+
497+
string to_string(unsigned long long value) {
498+
char buf[sizeof("18446744073709551615")];
499+
std::sprintf(buf, "%llu", value);
500+
return string(buf);
501+
}
502+
503+
#endif // _EZ80
504+
362505
#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
363506
wstring to_wstring(int val) { return i_to_string<wstring>(val); }
364507
wstring to_wstring(long val) { return i_to_string<wstring>(val); }
@@ -368,10 +511,34 @@ wstring to_wstring(unsigned long val) { return i_to_string<wstring>(val); }
368511
wstring to_wstring(unsigned long long val) { return i_to_string<wstring>(val); }
369512
#endif
370513

514+
#ifndef _EZ80
515+
371516
string to_string (float val) { return as_string(snprintf, initial_string< string>()(), "%f", val); }
372517
string to_string (double val) { return as_string(snprintf, initial_string< string>()(), "%f", val); }
373518
string to_string (long double val) { return as_string(snprintf, initial_string< string>()(), "%Lf", val); }
374519

520+
#else // _EZ80
521+
522+
string to_string(float value) {
523+
char buf[64];
524+
std::snprintf(buf, sizeof(buf), "%f", value);
525+
return string(buf);
526+
}
527+
528+
string to_string(double value) {
529+
char buf[64];
530+
std::snprintf(buf, sizeof(buf), "%f", value);
531+
return string(buf);
532+
}
533+
534+
string to_string(long double value) {
535+
char buf[64];
536+
std::snprintf(buf, sizeof(buf), "%Lf", value);
537+
return string(buf);
538+
}
539+
540+
#endif // _EZ80
541+
375542
#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
376543
wstring to_wstring(float val) { return as_string(get_swprintf(), initial_string<wstring>()(), L"%f", val); }
377544
wstring to_wstring(double val) { return as_string(get_swprintf(), initial_string<wstring>()(), L"%f", val); }

0 commit comments

Comments
 (0)