about summary refs log tree commit diff stats
path: root/407print-int32-decimal-right-justified.mu
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2020-09-22 10:13:35 -0700
committerKartik Agaram <vc@akkartik.com>2020-09-22 10:13:35 -0700
commit670bf6ed01b539868e13c7c8f2b8a33755289e51 (patch)
tree25ab45349f0db078f26ad06bf3f930546dd52d96 /407print-int32-decimal-right-justified.mu
parent8152b0d109a3ee01b7a357926aa01b1a8de3f366 (diff)
downloadmu-670bf6ed01b539868e13c7c8f2b8a33755289e51.tar.gz
6833 - tile: right-justify for negative numbers
Diffstat (limited to '407print-int32-decimal-right-justified.mu')
-rw-r--r--407print-int32-decimal-right-justified.mu78
1 files changed, 46 insertions, 32 deletions
diff --git a/407print-int32-decimal-right-justified.mu b/407print-int32-decimal-right-justified.mu
index d9b7e5b6..7d83217a 100644
--- a/407print-int32-decimal-right-justified.mu
+++ b/407print-int32-decimal-right-justified.mu
@@ -1,12 +1,12 @@
-# print n with enough leading spaces to be right-justified with max
-# only works for positive ints for now
-fn print-int32-decimal-right-justified screen: (addr screen), n: int, max: int {
-  var threshold/eax: int <- right-justify-threshold max
+# print 'n' with enough leading spaces to be right-justified with 'threshold'
+# 'threshold' should be the minimum positive number for some width
+fn print-int32-decimal-right-justified screen: (addr screen), n: int, _threshold: int {
+  # tweak things for negative numbers
+  var n2/ecx: int <- right-justify-threshold-decimal n
+  var threshold/eax: int <- copy _threshold
   {
-#?     print-int32-decimal screen, threshold
-    compare n, threshold
+    compare n2, threshold
     break-if->=
-#?     print-string screen, "!"
     print-grapheme screen, 0x20  # space
     threshold <- try-divide threshold, 0xa
     loop
@@ -14,39 +14,53 @@ fn print-int32-decimal-right-justified screen: (addr screen), n: int, max: int {
   print-int32-decimal screen, n
 }
 
-fn right-justify-threshold n: int -> result/eax: int {
+# return the minimum positive number with the same width in decimal as 'n'
+fn right-justify-threshold-decimal n: int -> result/ecx: int {
+  var ten/edx: int <- copy 0xa  # constant
+  # replace '-' at the start with '0' at the end
   var curr/eax: int <- copy n
-  var out/esi: int <- copy 1
-  var ten/ecx: int <- copy 0xa  # constant
+  compare curr, 0
+  {
+    break-if->=
+    curr <- negate
+    curr <- multiply ten
+  }
+  # now we're dealing with a positive number
+  result <- copy 1
   {
     compare curr, 0xa
     break-if-<
     curr <- try-divide curr, 0xa
-    out <- multiply ten
+    result <- multiply ten
     loop
   }
-  result <- copy out
 }
 
 fn test-right-justify-threshold {
-  var x/eax: int <- right-justify-threshold 0
-  check-ints-equal x, 1, "F - test-right-justify-threshold: 0"
-  x <- right-justify-threshold 1
-  check-ints-equal x, 1, "F - test-right-justify-threshold: 1"
-  x <- right-justify-threshold 4
-  check-ints-equal x, 1, "F - test-right-justify-threshold: 4"
-  x <- right-justify-threshold 9
-  check-ints-equal x, 1, "F - test-right-justify-threshold: 9"
-  x <- right-justify-threshold 0xa
-  check-ints-equal x, 0xa, "F - test-right-justify-threshold: 10"
-  x <- right-justify-threshold 0xb
-  check-ints-equal x, 0xa, "F - test-right-justify-threshold: 11"
-  x <- right-justify-threshold 0x4f  # 79
-  check-ints-equal x, 0xa, "F - test-right-justify-threshold: 79"
-  x <- right-justify-threshold 0x64  # 100
-  check-ints-equal x, 0x64, "F - test-right-justify-threshold: 100"
-  x <- right-justify-threshold 0x3e7  # 999
-  check-ints-equal x, 0x64, "F - test-right-justify-threshold: 999"
-  x <- right-justify-threshold 0x3e8  # 1000
-  check-ints-equal x, 0x3e8, "F - test-right-justify-threshold: 1000"
+  var x/ecx: int <- right-justify-threshold-decimal 0
+  check-ints-equal x, 1, "F - test-right-justify-threshold-decimal: 0"
+  x <- right-justify-threshold-decimal 1
+  check-ints-equal x, 1, "F - test-right-justify-threshold-decimal: 1"
+  x <- right-justify-threshold-decimal 4
+  check-ints-equal x, 1, "F - test-right-justify-threshold-decimal: 4"
+  x <- right-justify-threshold-decimal 9
+  check-ints-equal x, 1, "F - test-right-justify-threshold-decimal: 9"
+  x <- right-justify-threshold-decimal 0xa
+  check-ints-equal x, 0xa, "F - test-right-justify-threshold-decimal: 10"
+  x <- right-justify-threshold-decimal 0xb
+  check-ints-equal x, 0xa, "F - test-right-justify-threshold-decimal: 11"
+  x <- right-justify-threshold-decimal 0x4f  # 79
+  check-ints-equal x, 0xa, "F - test-right-justify-threshold-decimal: 79"
+  x <- right-justify-threshold-decimal 0x64  # 100
+  check-ints-equal x, 0x64, "F - test-right-justify-threshold-decimal: 100"
+  x <- right-justify-threshold-decimal 0x65  # 101
+  check-ints-equal x, 0x64, "F - test-right-justify-threshold-decimal: 101"
+  x <- right-justify-threshold-decimal 0x3e7  # 999
+  check-ints-equal x, 0x64, "F - test-right-justify-threshold-decimal: 999"
+  x <- right-justify-threshold-decimal 0x3e8  # 1000
+  check-ints-equal x, 0x3e8, "F - test-right-justify-threshold-decimal: 1000"
+  x <- right-justify-threshold-decimal -1
+  check-ints-equal x, 0xa, "F - test-right-justify-threshold-decimal: -1"
+  x <- right-justify-threshold-decimal -0xb  # -11
+  check-ints-equal x, 0x64, "F - test-right-justify-threshold-decimal: -11"
 }