This PR enforces rules around arithmetic of `String.Pos.Raw`.
Specifically, it adopts the following conventions:
- Byte indices ("ordinals") in strings should be represented using
`String.Pos.Raw`
- Amounts of bytes ("cardinals") in strings should be represented using
`Nat`.
For example, `String.Slice.utf8ByteSize` now returns `Nat` instead of
`String.Pos.Raw`, and there is a new function `String.Slice.rawEndPos`.
Finally, the `HAdd` and `HSub` instances for `String.Pos.Raw` are
reorganized. This is a **breaking change**.
The `HAdd/HSub String.Pos.Raw String.Pos.Raw String.Pos.Raw` instances
have been removed. For the use case of tracking positions relative to
some other position, we instead provide `offsetBy` and `unoffsetBy`
functions. For the use case of advancing/unadvancing a position by an
arbitrary number of bytes, we instead provide `increaseBy` and
`decreaseBy` functions. For
offsetting/unoffsetting/advancing/unadvancing a position `p` by the size
of a string `s` (resp. character `c`), use `s + p`/`p - s`/`p + s`/`p -
s` (resp. `c + p`/`p - c`/`p + c`/`p - c`).
29 lines
1 KiB
Text
29 lines
1 KiB
Text
def showChars : Nat → String → String.Pos.Raw → IO Unit
|
||
| 0, _, _ => pure ()
|
||
| n+1, s, idx => do
|
||
unless s.atEnd idx do
|
||
IO.println (">> " ++ toString (s.get idx)) *>
|
||
showChars n s (s.next idx)
|
||
|
||
def main : IO UInt32 :=
|
||
let s₁ := "hello α_world_β";
|
||
let b : String.Pos.Raw := 0;
|
||
let e := s₁.endPos;
|
||
IO.println (s₁.extract b e) *>
|
||
IO.println (s₁.extract (b+ " ") e) *>
|
||
IO.println (s₁.extract (b+ " ") (e.unoffsetBy ⟨1⟩)) *>
|
||
IO.println (s₁.extract (b.offsetBy ⟨2⟩) (e.unoffsetBy ⟨2⟩)) *>
|
||
IO.println (s₁.extract (b.offsetBy ⟨7⟩) e) *>
|
||
IO.println (s₁.extract (b.offsetBy ⟨8⟩) e) *>
|
||
IO.println (toString e) *>
|
||
IO.println (repr " aaa ".trim) *>
|
||
showChars s₁.length s₁ 0 *>
|
||
IO.println ("abc".isPrefixOf "abcd") *>
|
||
IO.println ("abcd".isPrefixOf "abcd") *>
|
||
IO.println ("".isPrefixOf "abcd") *>
|
||
IO.println ("".isPrefixOf "") *>
|
||
IO.println ("ab".isPrefixOf "cb") *>
|
||
IO.println ("ab".isPrefixOf "a") *>
|
||
IO.println ("αb".isPrefixOf "αbc") *>
|
||
IO.println ("\x00a").length *>
|
||
pure 0
|