Merge pull request #2512 from spnethw/tmppanel_fix_menu_from_file_list_crash
[far2l.git] / utils / src / IntStrConv.cpp
blob8bf7891f5beb4cb9b3cb19e45c5c930615fd6e8c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <wchar.h>
4 #include "utils.h"
6 extern "C"
8 char *itoa(int i, char *a, int radix)
10 switch (radix) {
11 case 10: sprintf(a, "%d", i); break;
12 case 16: sprintf(a, "%x", i); break;
14 return a;
17 int _wtoi(const wchar_t *w)
19 wchar_t *endptr = 0;
20 return wcstol(w, &endptr, 10);
23 int64_t _wtoi64(const wchar_t *w)
25 wchar_t *endptr = 0;
26 return wcstoll(w, &endptr, 10);
29 char * _i64toa(int64_t i, char *a, int radix)
31 switch (radix) {
32 case 10: sprintf(a, "%lld", (long long)i); break;
33 case 16: sprintf(a, "%llx", (long long)i); break;
35 return a;
38 wchar_t * _i64tow(int64_t i, wchar_t *w, int radix)
40 int neg;
41 if (i==0) {
42 w[0] = '0';
43 w[1] = 0;
44 return w;
47 neg = i < 0;
48 if (neg) {
49 *w++ = '-';
50 i = -i;
53 for (int64_t j = i; j; j/= radix) ++w;
54 *w = 0;
55 for (; i; i/= radix) {
56 unsigned int d = i % radix;
57 --w;
58 if (d<=9) *w = d + '0';
59 else *w = d - 10 + 'a';
62 return neg ? w-1 : w;
66 wchar_t * _itow(int i, wchar_t *w, int radix)
68 int neg;
69 if (i==0) {
70 w[0] = '0';
71 w[1] = 0;
72 return w;
74 neg = i<0;
75 if (neg) {
76 *w++ = '-';
77 i = -i;
80 for (int j = i; j; j/= radix) ++w;
81 *w = 0;
82 for (; i; i/= radix) {
83 unsigned int d = i % radix;
84 --w;
85 if (d<=9) *w = d + '0';
86 else *w = d - 10 + 'a';
89 return neg ? w-1 : w;
94 unsigned long HexToULong(const char *str, size_t maxlen, size_t *pos)
96 unsigned long out = 0;
97 size_t i;
98 for (i = pos ? *pos : 0; i < maxlen; ++i) {
99 unsigned char x = ParseHexDigit(str[i]);
100 if (x == 0xff) {
101 break;
103 out<<= 4;
104 out|= (unsigned long)x;
106 if (pos) {
107 *pos = i;
110 return out;
113 unsigned long DecToULong(const char *str, size_t maxlen, size_t *pos)
115 unsigned long out = 0;
116 size_t i;
117 for (i = pos ? *pos : 0; i < maxlen; ++i) {
118 const auto ch = str[i];
119 if (ch < '0' || ch > '9') {
120 break;
122 out*= 10;
123 out+= ch - '0';
125 if (pos) {
126 *pos = i;
128 return out;
131 long DecToLong(const char *str, size_t maxlen, size_t *pos)
133 const bool minus = maxlen && *str == '-';
134 long out = DecToULong(minus ? str + 1 : str, minus ? maxlen - 1 : maxlen, pos);
135 if (minus) {
136 if (pos) {
137 ++*pos;
139 out = -out;
141 return out;
144 bool IsHexaDecimalNumberStr(const char *str)
146 if (!*str) {
147 return false;
150 for (;*str; ++str) {
151 if ((*str < '0' || *str > '9')
152 && (*str < 'a' || *str > 'f')
153 && (*str < 'A' || *str > 'F')) {
154 return false;
158 return true;
161 char MakeHexDigit(const unsigned char c)
163 if (c <= 9) {
164 return '0' + c;
167 if (c <= 0xf) {
168 return 'a' + (c - 0xa);
171 return 0;
175 static void AppendHex(std::string &s, uint64_t v)
177 if (v == 0) {
178 s+= '0';
179 return;
182 const size_t prev_sz = s.size();
183 size_t i = 0;
184 for (uint64_t tmp = v; tmp; tmp/= 16) {
185 ++i;
187 s.resize(prev_sz + i);
188 for (uint64_t tmp = v; tmp; tmp/= 16) {
189 --i;
190 s[prev_sz + i] = MakeHexDigit((unsigned char)(tmp % 16));
194 std::string ToHex(uint64_t v)
196 std::string s;
197 AppendHex(s, v);
198 return s;
201 std::string ToPrefixedHex(uint64_t v, const char *prefix)
203 std::string s(prefix);
204 AppendHex(s, v);
205 return s;