about summary refs log tree commit diff stats
path: root/311decimal-int.subx
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2020-12-01 00:24:00 -0800
committerKartik Agaram <vc@akkartik.com>2020-12-01 00:32:43 -0800
commita06611f35cf39d201fb2581c81e56c7a8b7ddee6 (patch)
tree42c38fc24eab12dfe7d5a17e76b1bbbaf15b6e57 /311decimal-int.subx
parent8a73ef705207566e8d32a57bbb513f714e8fb9d6 (diff)
downloadmu-a06611f35cf39d201fb2581c81e56c7a8b7ddee6.tar.gz
7310 - advent day 1 part 1
In the process I had to:
* Fix a bug in reading lines from stdin into streams
* Start trailing newlines when parsing ints from streams
* Implement `find` in an array
Diffstat (limited to '311decimal-int.subx')
-rw-r--r--311decimal-int.subx12
1 files changed, 12 insertions, 0 deletions
diff --git a/311decimal-int.subx b/311decimal-int.subx
index 62ec51d6..badae601 100644
--- a/311decimal-int.subx
+++ b/311decimal-int.subx
@@ -60,6 +60,18 @@ parse-decimal-int-from-stream:  # in: (addr stream byte) -> result/eax: int
     # var end/edx: (addr byte) = &in->data[in->write]
     8b/-> *eax 2/r32/edx
     8d/copy-address *(eax+edx+0xc) 2/r32/edx
+    # trim a trailing newline
+    {
+      # speculatively trim
+      4a/decrement-edx
+      # if it's a newline, break
+      8a/byte-> *edx 0/r32/eax
+      81 4/subop/and %eax 0xff/imm32
+      3d/compare-eax-and 0xa/imm32/newline
+      74/jump-if-= break/disp8
+      # not a newline, so restore it
+      42/increment-edx
+    }
     #
     (parse-decimal-int-helper %ecx %edx)  # => eax
 $parse-decimal-int-from-stream:end: