add @string.Index and refactor json parser #1631
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The first commit adds the type
@string.Index
. Now there are three accessing methods forString
:nth_charcode(String, Int) -> Int
// access n-th charcode in O(1) timenth_codepoint(String, Int) -> Char
// access n-th codepoint in O(n) timecodepoint_at(String, @string.Index) -> Char
// access codepoint at given index in O(1) timeWe might need another one for completeness but I'm not sure about whether it's necessary:
charcode_at(String, @string.Index) -> Int
// access charcode at given index in O(1) timeThe problem with the fourth method is that it needs another parameter
shift~ : Int
so that it can access the trailing surrogate.And for taking view, there are two methods:
op_as_view(String, Int, Int) -> @string.View
// taking the view by skipping a number of codepoints on both endsas_view(String, @string.Index, @string.Index) -> @string.View
// taking the view by just using the given indexThe second commit shows how to use these methods to rewrite the json parser. The main benefit here is that there's no manual checking of surrogate pairs without sacrificing the efficiency.