strconv.odin 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. package strconv
  2. import "core:unicode/utf8"
  3. parse_bool :: proc(s: string) -> (result: bool = false, ok: bool) {
  4. switch s {
  5. case "1", "t", "T", "true", "TRUE", "True":
  6. return true, true;
  7. case "0", "f", "F", "false", "FALSE", "False":
  8. return false, true;
  9. }
  10. return;
  11. }
  12. _digit_value :: proc(r: rune) -> int {
  13. ri := int(r);
  14. v: int = 16;
  15. switch r {
  16. case '0'..'9': v = ri-'0';
  17. case 'a'..'z': v = ri-'a'+10;
  18. case 'A'..'Z': v = ri-'A'+10;
  19. }
  20. return v;
  21. }
  22. parse_i64 :: proc(str: string) -> i64 {
  23. s := str;
  24. neg := false;
  25. if len(s) > 1 {
  26. switch s[0] {
  27. case '-':
  28. neg = true;
  29. s = s[1:];
  30. case '+':
  31. s = s[1:];
  32. }
  33. }
  34. base: i64 = 10;
  35. if len(s) > 2 && s[0] == '0' {
  36. switch s[1] {
  37. case 'b': base = 2; s = s[2:];
  38. case 'o': base = 8; s = s[2:];
  39. case 'd': base = 10; s = s[2:];
  40. case 'z': base = 12; s = s[2:];
  41. case 'x': base = 16; s = s[2:];
  42. }
  43. }
  44. value: i64;
  45. for r in s {
  46. if r == '_' {
  47. continue;
  48. }
  49. v := i64(_digit_value(r));
  50. if v >= base {
  51. break;
  52. }
  53. value *= base;
  54. value += v;
  55. }
  56. if neg do return -value;
  57. return value;
  58. }
  59. parse_u64 :: proc(str: string) -> u64 {
  60. s := str;
  61. neg := false;
  62. if len(s) > 1 && s[0] == '+' {
  63. s = s[1:];
  64. }
  65. base := u64(10);
  66. if len(s) > 2 && s[0] == '0' {
  67. switch s[1] {
  68. case 'b': base = 2; s = s[2:];
  69. case 'o': base = 8; s = s[2:];
  70. case 'd': base = 10; s = s[2:];
  71. case 'z': base = 12; s = s[2:];
  72. case 'x': base = 16; s = s[2:];
  73. }
  74. }
  75. value: u64;
  76. for r in s {
  77. if r == '_' do continue;
  78. v := u64(_digit_value(r));
  79. if v >= base do break;
  80. value *= base;
  81. value += u64(v);
  82. }
  83. if neg do return -value;
  84. return value;
  85. }
  86. parse_int :: proc(s: string) -> int {
  87. return int(parse_i64(s));
  88. }
  89. parse_uint :: proc(s: string) -> uint {
  90. return uint(parse_u64(s));
  91. }
  92. parse_f32 :: proc(s: string) -> f32 {
  93. return f32(parse_f64(s));
  94. }
  95. parse_f64 :: proc(s: string) -> f64 {
  96. if s == "" {
  97. return 0;
  98. }
  99. i := 0;
  100. sign: f64 = 1;
  101. switch s[i] {
  102. case '-': i += 1; sign = -1;
  103. case '+': i += 1;
  104. }
  105. value: f64 = 0;
  106. for ; i < len(s); i += 1 {
  107. r := rune(s[i]);
  108. if r == '_' do continue;
  109. v := _digit_value(r);
  110. if v >= 10 do break;
  111. value *= 10;
  112. value += f64(v);
  113. }
  114. if i < len(s) && s[i] == '.' {
  115. pow10: f64 = 10;
  116. i += 1;
  117. for ; i < len(s); i += 1 {
  118. r := rune(s[i]);
  119. if r == '_' do continue;
  120. v := _digit_value(r);
  121. if v >= 10 do break;
  122. value += f64(v)/pow10;
  123. pow10 *= 10;
  124. }
  125. }
  126. frac := false;
  127. scale: f64 = 1;
  128. if i < len(s) && (s[i] == 'e' || s[i] == 'E') {
  129. i += 1;
  130. if i < len(s) {
  131. switch s[i] {
  132. case '-': i += 1; frac = true;
  133. case '+': i += 1;
  134. }
  135. exp: u32 = 0;
  136. for ; i < len(s); i += 1 {
  137. r := rune(s[i]);
  138. if r == '_' do continue;
  139. d := u32(_digit_value(r));
  140. if d >= 10 do break;
  141. exp = exp * 10 + d;
  142. }
  143. if exp > 308 { exp = 308; }
  144. for exp >= 50 { scale *= 1e50; exp -= 50; }
  145. for exp >= 8 { scale *= 1e8; exp -= 8; }
  146. for exp > 0 { scale *= 10; exp -= 1; }
  147. }
  148. }
  149. if frac do return sign * (value/scale);
  150. return sign * (value*scale);
  151. }
  152. append_bool :: proc(buf: []byte, b: bool) -> string {
  153. n := 0;
  154. if b do n = copy(buf, "true");
  155. else do n = copy(buf, "false");
  156. return string(buf[:n]);
  157. }
  158. append_uint :: proc(buf: []byte, u: u64, base: int) -> string {
  159. return append_bits(buf, u64(u), base, false, 8*size_of(uint), digits, nil);
  160. }
  161. append_int :: proc(buf: []byte, i: i64, base: int) -> string {
  162. return append_bits(buf, u64(i), base, true, 8*size_of(int), digits, nil);
  163. }
  164. itoa :: proc(buf: []byte, i: int) -> string {
  165. return append_int(buf, i64(i), 10);
  166. }
  167. atoi :: proc(s: string) -> int {
  168. return parse_int(s);
  169. }
  170. atof :: proc(s: string) -> f64 {
  171. return parse_f64(s);
  172. }
  173. ftoa :: append_float;
  174. append_float :: proc(buf: []byte, f: f64, fmt: byte, prec, bit_size: int) -> string {
  175. return string(generic_ftoa(buf, f, fmt, prec, bit_size));
  176. }
  177. quote :: proc(buf: []byte, str: string) -> string {
  178. write_byte :: inline proc(buf: []byte, i: ^int, bytes: ..byte) {
  179. if i^ >= len(buf) do return;
  180. n := copy(buf[i^:], bytes[:]);
  181. i^ += n;
  182. }
  183. if buf == nil {
  184. return "";
  185. }
  186. c :: '"';
  187. i := 0;
  188. s := str;
  189. write_byte(buf, &i, c);
  190. for width := 0; len(s) > 0; s = s[width:] {
  191. r := rune(s[0]);
  192. width = 1;
  193. if r >= utf8.RUNE_SELF {
  194. r, width = utf8.decode_rune_in_string(s);
  195. }
  196. if width == 1 && r == utf8.RUNE_ERROR {
  197. write_byte(buf, &i, '\\', 'x');
  198. write_byte(buf, &i, digits[s[0]>>4]);
  199. write_byte(buf, &i, digits[s[0]&0xf]);
  200. }
  201. if i < len(buf) {
  202. x := quote_rune(buf[i:], r);
  203. i += len(x);
  204. }
  205. }
  206. write_byte(buf, &i, c);
  207. return string(buf[:i]);
  208. }
  209. quote_rune :: proc(buf: []byte, r: rune) -> string {
  210. write_byte :: inline proc(buf: []byte, i: ^int, bytes: ..byte) {
  211. if i^ < len(buf) {
  212. n := copy(buf[i^:], bytes[:]);
  213. i^ += n;
  214. }
  215. }
  216. write_string :: inline proc(buf: []byte, i: ^int, s: string) {
  217. if i^ < len(buf) {
  218. n := copy(buf[i^:], s);
  219. i^ += n;
  220. }
  221. }
  222. write_rune :: inline proc(buf: []byte, i: ^int, r: rune) {
  223. if i^ < len(buf) {
  224. b, w := utf8.encode_rune(r);
  225. n := copy(buf[i^:], b[:w]);
  226. i^ += n;
  227. }
  228. }
  229. if buf == nil {
  230. return "";
  231. }
  232. i := 0;
  233. write_byte(buf, &i, '\'');
  234. switch r {
  235. case '\a': write_string(buf, &i, "\\a");
  236. case '\b': write_string(buf, &i, "\\b");
  237. case '\e': write_string(buf, &i, "\\e");
  238. case '\f': write_string(buf, &i, "\\f");
  239. case '\n': write_string(buf, &i, "\\n");
  240. case '\r': write_string(buf, &i, "\\r");
  241. case '\t': write_string(buf, &i, "\\t");
  242. case '\v': write_string(buf, &i, "\\v");
  243. case:
  244. if r < 32 {
  245. write_string(buf, &i, "\\x");
  246. b: [2]byte;
  247. s := append_bits(b[:], u64(r), 16, true, 64, digits, nil);
  248. switch len(s) {
  249. case 0: write_string(buf, &i, "00");
  250. case 1: write_rune(buf, &i, '0');
  251. case 2: write_string(buf, &i, s);
  252. }
  253. } else {
  254. write_rune(buf, &i, r);
  255. }
  256. }
  257. write_byte(buf, &i, '\'');
  258. return string(buf[:i]);
  259. }