@@ -12,21 +12,21 @@ namespace Unicode
1212 around field values are not significant. Line comments are prefixed with a
1313 number sign `#` (U+0023).
1414-/
15- structure UCDStream extends Substring.Raw where
15+ structure UCDStream extends String.Slice where
1616 /-- `isUnihan` is true if the records are tab separated -/
1717 isUnihan := false
1818deriving Inhabited
1919
2020namespace UCDStream
2121
22- /-- Make a `UCDStream` from a substring -/
23- def ofSubstring (str : Substring.Raw ) : UCDStream := { str with }
22+ /-- Make a `UCDStream` from a string slice -/
23+ def ofStringSlice (str : String.Slice ) : UCDStream := { str with }
2424
2525/-- Make a `UCDStream` from a string -/
26- def ofString (str : String) : UCDStream where
27- str := str
28- startPos := 0
29- stopPos := str.rawEndPos
26+ def ofString (str : String) : UCDStream := ofStringSlice str.toSlice
27+
28+ /-- Make a `UCDStream` from a substring -/
29+ def ofSubstring (str : Substring.Raw) : UCDStream := ofStringSlice str.toString.toSlice
3030
3131/-- Make a `UCDStream` from a file -/
3232def ofFile (path : System.FilePath) : IO UCDStream :=
@@ -36,29 +36,30 @@ def ofFile (path : System.FilePath) : IO UCDStream :=
3636
3737 Line comments are stripped and blank lines are skipped.
3838-/
39- protected partial def nextLine ? (stream : UCDStream) : Option (Substring.Raw × UCDStream) := do
40- if stream.isEmpty then failure else
41- let line := stream.trimLeft.takeWhile (.!= ' \n ' )
42- let nextPos := stream.next line.stopPos
43- let line := (line.takeWhile (.!='#' )).trimRight
39+ protected partial def nextLine ? (stream : UCDStream) : Option (String.Slice × UCDStream) := do
40+ let line := stream.trimAsciiEnd.takeWhile (.!= ' \n ' )
41+ if h : line.rawEndPos < stream.rawEndPos then
42+ let nextPos := stream.findNextPos line.rawEndPos h
43+ let line := (line.takeWhile (.!='#' )).trimAsciiEnd
4444 if line.isEmpty then
45- UCDStream.nextLine? {stream with startPos := nextPos}
45+ UCDStream.nextLine? {stream with toSlice := stream.replaceStart nextPos}
4646 else
47- return (line, {stream with startPos := nextPos})
47+ return (line, {stream with toSlice := stream.replaceStart nextPos})
48+ else failure
4849
4950/-- Get the next record from the `UCDStream`
5051
5152 Spaces around field values are trimmed.
5253-/
53- protected def next ? (stream : UCDStream) : Option (Array Substring.Raw × UCDStream) := do
54+ protected def next ? (stream : UCDStream) : Option (Array String.Slice × UCDStream) := do
5455 let sep := if stream.isUnihan then " \t " else " ;"
5556 let mut arr := #[]
5657 let (line, table) ← stream.nextLine?
57- for item in line.splitOn sep do
58- arr := arr.push item.trim
58+ for item in line.split sep do
59+ arr := arr.push item.trimAscii
5960 return (arr, table)
6061
61- instance : Std.Stream UCDStream (Array Substring.Raw ) where
62+ instance : Std.Stream UCDStream (Array String.Slice ) where
6263 next? := UCDStream.next?
6364
6465end UCDStream
0 commit comments