aboutsummaryrefslogtreecommitdiff
path: root/examples/basic.odin
diff options
context:
space:
mode:
authorgingerBill <ginger.bill.22@gmail.com>2016-08-05 15:17:23 +0100
committergingerBill <ginger.bill.22@gmail.com>2016-08-05 15:17:23 +0100
commit4a303b5c3ef38bd99c36fa990c922917c0134d52 (patch)
treefc46614cd8b13cbb84228911209fd505d46370b6 /examples/basic.odin
parent2aaef48c5c362bb3e04d0c9cd1e722e21b3755e5 (diff)
Minor refactor and basic library
Diffstat (limited to 'examples/basic.odin')
-rw-r--r--examples/basic.odin39
1 files changed, 39 insertions, 0 deletions
diff --git a/examples/basic.odin b/examples/basic.odin
index 1c5b7b317..1f90efa22 100644
--- a/examples/basic.odin
+++ b/examples/basic.odin
@@ -6,3 +6,42 @@ print_string :: proc(s : string) {
putchar(c);
}
}
+
+string_byte_reverse :: proc(s : string) {
+ n := len(s);
+ for i := 0; i < n/2; i++ {
+ s[i], s[n-1-i] = s[n-1-i], s[i];
+ }
+}
+
+print_int :: proc(i : int, base : int) {
+ NUM_TO_CHAR_TABLE := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@$";
+
+ buf : [21]byte;
+ len := 0;
+ negative := false;
+ if i < 0 {
+ negative = true;
+ i = -i;
+ }
+ if i > 0 {
+ for i > 0 {
+ c : byte = NUM_TO_CHAR_TABLE[i % base];
+ buf[len] = c;
+ len++;
+ i /= base;
+ }
+ } else {
+ buf[len] = '0';
+ len++;
+ }
+
+ if negative {
+ buf[len] = '-';
+ len++;
+ }
+
+ str := cast(string)buf[:len];
+ string_byte_reverse(str);
+ print_string(str);
+}