Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use string cursor instead of primitive uncons #10

Merged
merged 2 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
nix_path: nixpkgs=channel:nixos-unstable

- name: Setup PureScript dependencies
run: npm i --global [email protected] spago@next purescm@next
run: npm i --global [email protected] spago@next purescm@latest

- name: Build source
run: spago build
Expand Down
43 changes: 22 additions & 21 deletions strings/src/Data/String/CodePoints.ss
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
pstring-length-code-points
pstring-ref-code-point
pstring-take-code-points
pstring-uncons-code-point
string->pstring)
pstring-cursor-read-code-point
pstring->cursor
cursor->pstring)
(only (chezscheme) fx1+ fx=?))

(define length pstring-length-code-points)
Expand All @@ -33,24 +34,23 @@
(lambda (Nothing)
(lambda (index)
(lambda (s)
(let loop ([i 0] [cur s])
(if (pstring-empty? cur)
Nothing
(let-values ([(head tail) (pstring-uncons-code-point cur)])
(if (fx=? i index)
(Just head)
(loop (fx1+ i) tail))))))))))
(let ([cur (pstring->cursor s)])
(let loop ([i 0] [cp (pstring-cursor-read-code-point cur)])
(cond
[(eof-object? cp) Nothing]
[(fx=? i index) (Just cp)]
[else (loop (fx1+ i) (pstring-cursor-read-code-point cur))]))))))))

(define countPrefix
(lambda (pred)
(lambda (s)
(let loop ([count 0] [rest s])
(if (pstring-empty? rest)
count
(let-values ([(head tail) (pstring-uncons-code-point rest)])
(if (pred head)
(loop (fx1+ count) tail)
count)))))))
(let ([cursor (pstring->cursor s)])
(let loop ([count 0])
(let ([cp (pstring-cursor-read-code-point cursor)])
(cond
[(eof-object? cp) count]
[(pred cp) (loop (fx1+ count))]
[else count])))))))

(define fromCodePointArray
(lambda (cps)
Expand All @@ -67,12 +67,13 @@
(lambda (Just)
(lambda (Nothing)
(lambda (s)
(if (pstring-empty? s)
Nothing
(let-values ([(c tail) (pstring-uncons-code-point s)])
(let* ([cur (pstring->cursor s)]
[cp (pstring-cursor-read-code-point cur)])
(if (eof-object? cp)
Nothing
(Just (list
(cons 'head c)
(cons 'tail tail)))))))))
(cons 'head cp)
(cons 'tail (cursor->pstring cur))))))))))

(define toCodePointArray pstring->code-point-flexvector)

Expand Down
Loading