Skip to content

Commit

Permalink
Rename sh-runtime var used to store strings
Browse files Browse the repository at this point in the history
When arithmetic expansion is used to declare a
variable, zsh types the variable as "integer"
instead of "scalar". This is a problem when the
variable is later assigned a string value, as zsh
enforces the integer type and throws an error.

```
: $((__buf = 12)) # line 4: bad math expression: operator expected at `a', __buf has type"integer"
print -rl -- ${(t)__buf}
echo "__buf: $__buf =? 0123456789abcdef"
__buf="0a"
```
  • Loading branch information
laurenthuberdeau committed Feb 20, 2025
1 parent bc734b4 commit 4f6e1da
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions sh-runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -450,20 +450,20 @@ DEPENDS_ON(malloc)
DEPENDS_ON(char_to_int)
#ifdef OPTIMIZE_LONG_LINES
putstr("next_sub_buffer() {\n");
extract_line_head(" ", "__us_buf256", "__buf", ANY_STRING_256, "256", "")
extract_line_head(" ", "__us_buf256", "__str", ANY_STRING_256, "256", "")
extract_line_head(" ", "__us_buf16", "__us_buf256", ANY_STRING_16, "16", "")
putstr("}\n");
#endif
putstr("unpack_escaped_string() { # $1 = string, $2 = size (optional)\n");
putstr(" __buf=\"$1\"\n");
putstr(" __str=\"$1\"\n");
putstr(" # Allocates enough space for all characters, assuming that no character is escaped\n");
putstr(" _malloc __addr $((${2:-${#__buf} + 1}))\n");
putstr(" _malloc __addr $((${2:-${#__str} + 1}))\n");
putstr(" __ptr=$__addr\n");
putstr(" __end=$((__ptr + ${2:-${#__buf} + 1})) # End of allocated memory\n");
putstr(" __end=$((__ptr + ${2:-${#__str} + 1})) # End of allocated memory\n");
#ifdef OPTIMIZE_LONG_LINES
putstr(" __us_buf16=\n");
putstr(" __us_buf256=\n");
putstr(" while [ ! -z \"$__buf\" ] || [ ! -z \"$__us_buf256\" ] ; do\n");
putstr(" while [ ! -z \"$__str\" ] || [ ! -z \"$__us_buf256\" ] ; do\n");
putstr(" next_sub_buffer\n");
putstr(" while [ ! -z \"$__us_buf16\" ]; do\n");
handle_escaped_chars(" ", "__us_buf16", "__c")
Expand All @@ -472,8 +472,8 @@ DEPENDS_ON(char_to_int)
putstr(" done\n");
putstr(" done\n");
#else
putstr(" while [ -n \"$__buf\" ] ; do\n");
handle_escaped_chars(" ", "__buf", "__c")
putstr(" while [ -n \"$__str\" ] ; do\n");
handle_escaped_chars(" ", "__str", "__c")
putstr(" : $((_$__ptr = __c))\n");
putstr(" : $((__ptr += 1))\n");
putstr(" done\n");
Expand Down

0 comments on commit 4f6e1da

Please sign in to comment.