Created
September 5, 2023 19:36
-
-
Save andrepiske/f871e6f3d16470a6b7046a1ce3c291a0 to your computer and use it in GitHub Desktop.
S3LineLogParser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class S3LineLogParser | |
| attr_reader :data, :cursor | |
| def initialize(data) | |
| @data = data | |
| @cursor = 0 | |
| end | |
| def read_all | |
| tokens = [] | |
| while @cursor < @data.length | |
| tokens << read_something | |
| end | |
| tokens | |
| end | |
| def read_something | |
| la = @data[@cursor] | |
| case la | |
| when '['; read_bracketed | |
| when '"'; read_quoted | |
| when ' '; raise "Error: found a space at #{@cursor}" | |
| else; read_plain | |
| end | |
| end | |
| def read_bracketed | |
| s = [] | |
| @cursor += 1 | |
| loop do | |
| c = @data[@cursor] | |
| @cursor += 1 | |
| if c == ']' || c == nil | |
| @cursor += 1 | |
| break | |
| end | |
| s << c | |
| end | |
| s.join | |
| end | |
| def read_quoted | |
| s = [] | |
| esc = nil | |
| @cursor += 1 | |
| loop do | |
| c = @data[@cursor] | |
| @cursor += 1 | |
| if esc | |
| if c == '"' | |
| s << c | |
| else | |
| raise "Unknown escape char: 0x#{c.ord.to_s(16)}." | |
| end | |
| esc = nil | |
| elsif c == '"' || c == nil | |
| @cursor += 1 | |
| break | |
| end | |
| s << c | |
| end | |
| s.join | |
| end | |
| def read_plain | |
| s = [] | |
| loop do | |
| c = @data[@cursor] | |
| @cursor += 1 | |
| break if c == " " || c == "\t" || c == nil | |
| s << c | |
| end | |
| s.join | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment