Skip to content

Commit

Permalink
fix string-set! to mutate the string and work on any expression
Browse files Browse the repository at this point in the history
  • Loading branch information
jcubic committed Jan 22, 2024
1 parent 47b0cb8 commit 1e1d027
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 40 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* add `typecheck-number` function
### Bugfix
* fix `let-values` to allow binding to list [#281](https://github.com/jcubic/lips/issues/281)
* fix `string-fill!`
* fix wrong strings in `string-fill!`
* fix `string-set!` to mutate the string and work on any expression

## 1.0.0-beta.18
### Breaking
Expand Down
10 changes: 5 additions & 5 deletions dist/std.min.scm

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 12 additions & 17 deletions dist/std.scm

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified dist/std.xcb
Binary file not shown.
25 changes: 10 additions & 15 deletions lib/R5RS.scm
Original file line number Diff line number Diff line change
Expand Up @@ -776,29 +776,24 @@
(lips.LCharacter x))))))

;; -----------------------------------------------------------------------------
;; (let ((x "hello")) (string-set! x 0 #\H) x)
(define-macro (string-set! object index char)
"(string-set! object index char)
"(string-set! string index char)
Replaces character in string in given index. It create new JavaScript
string and replaces the old value. Object needs to be symbol that points to the variable
that holds the string."
(typecheck "string-set!" object "symbol")
(let ((chars (gensym "chars")))
Replaces character in string at a given index."
(let ((input (gensym "input")))
`(begin
(typecheck "string-set!" ,object "string")
(typecheck "string-set!" ,index "number")
(typecheck "string-set!" ,char "character")
(let ((,chars (list->vector (string->list ,object))))
(set-obj! ,chars ,index ,char)
(set! ,object (list->string (vector->list ,chars)))))))
(let ((,input ,object))
(typecheck "string-set!" ,input "string")
(typecheck "string-set!" ,index "number")
(typecheck "string-set!" ,char "character")
(--> ,input (set ,index ,char))))))

;; -----------------------------------------------------------------------------
(define (string-length string)
"(string-length string)

Returns the length of the string."
(typecheck "string-ref" string "string")
(typecheck "string-length" string "string")
(. string 'length))

;; -----------------------------------------------------------------------------
Expand All @@ -808,7 +803,7 @@
Returns character inside string at given zero-based index."
(typecheck "string-ref" string "string" 1)
(typecheck "string-ref" k "number" 2)
(lips.LCharacter (--> string (get k))))
(lips.LCharacter (string.get k)))

(define (%string-cmp name string1 string2)
"(%string-cmp name a b)
Expand Down
4 changes: 2 additions & 2 deletions lib/R7RS.scm
Original file line number Diff line number Diff line change
Expand Up @@ -407,15 +407,15 @@

Function convert a string passed as argument to lower case."
(typecheck "string-downcase" string "string")
(string.toLowerCase))
(string.lower))

;; -----------------------------------------------------------------------------
(define (string-upcase string)
"(string-downcase string)

Function convert a string passed as argument to upper case."
(typecheck "string-downcase" string "string")
(string.toUpperCase))
(string.upper))

;; -----------------------------------------------------------------------------
(define (dynamic-wind before thunk after)
Expand Down

0 comments on commit 1e1d027

Please sign in to comment.