Port builtins/string to Rust

- Add test to verify piped string replace exit code

Ensure fields parsing error messages are the same.

Note: C++ relied upon the value of the parsed value even when `errno` was set,
that is defined behaviour we should not rely on, and cannot easilt be replicated from Rust.
Therefore the Rust version will change the following error behaviour from:

```shell
> string split --fields=a "" abc
string split: Invalid fields value 'a'
> string split --fields=1a "" abc
string split: 1a: invalid integer
```

To:

```shell
> string split --fields=a "" abc
string split: a: invalid integer
> string split --fields=1a "" abc
string split: 1a: invalid integer
```
This commit is contained in:
Henrik Hørlück Berg
2023-06-20 04:28:35 +02:00
committed by Peter Ammon
parent 2110b36426
commit 20be990fd9
39 changed files with 3061 additions and 3006 deletions

View File

@@ -199,3 +199,8 @@ abbr --add --regex foo --function foo
# CHECKERR: abbr --add: Name cannot be empty
echo foo
# CHECK: foo
abbr --add regex_name --regex '(*UTF).*' bar
# CHECKERR: abbr: Regular expression compile error: using UTF is disabled by the application
# CHECKERR: abbr: (*UTF).*
# CHECKERR: abbr: ^

View File

@@ -45,9 +45,13 @@ string length -q ""; and echo not zero length; or echo zero length
string pad foo
# CHECK: foo
string pad -r -w 7 -c - foo
string pad -r -w 7 --chars - foo
# CHECK: foo----
# might overflow when converting sign
string sub --start -9223372036854775808 abc
# CHECK: abc
string pad --width 7 -c '=' foo
# CHECK: ====foo
@@ -175,6 +179,10 @@ string split "" abc
# CHECK: b
# CHECK: c
string split --max 1 --right 12 "AB12CD"
# CHECK: AB
# CHECK: CD
string split --fields=2 "" abc
# CHECK: b
@@ -185,6 +193,39 @@ string split --fields=3,2 "" abc
string split --fields=2,9 "" abc; or echo "exit 1"
# CHECK: exit 1
string split --fields=2-3-,9 "" a
# CHECKERR: string split: 2-3-,9: invalid integer
string split --fields=1-99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 "" abc
# CHECKERR: string split: 1-99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999: invalid integer
string split --fields=99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999-1 "" abc
# CHECKERR: string split: 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999-1: invalid integer
string split --fields=1--2 "" b
# CHECKERR: string split: 1--2: invalid integer
string split --fields=0 "" c
# CHECKERR: string split: Invalid fields value '0'
string split --fields=99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 "" abc
# CHECKERR: string split: 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999: invalid integer
string split --fields=1-0 "" d
# CHECKERR: string split: Invalid range value for field '1-0'
string split --fields=0-1 "" e
# CHECKERR: string split: Invalid range value for field '0-1'
string split --fields=-1 "" f
# CHECKERR: string split: -1: invalid integer
string split --fields=1a "" g
# CHECKERR: string split: 1a: invalid integer
string split --fields=a "" h
# CHECKERR: string split: a: invalid integer
string split --fields=1-3,5,9-7 "" 123456789
# CHECK: 1
# CHECK: 2
@@ -359,6 +400,14 @@ string replace -r "\s*newline\s*" "\n" "put a newline here"
string replace -r -a "(\w)" "\$1\$1" ab
# CHECK: aabb
echo a | string replace b c -q
or echo No replace fails
# CHECK: No replace fails
echo a | string replace -r b c -q
or echo No replace regex fails
# CHECK: No replace regex fails
string replace --filter x X abc axc x def jkx
or echo Unexpected exit status at line (status --current-line-number)
# CHECK: aXc
@@ -468,6 +517,22 @@ string repeat -n 5 --max 4 123 '' 789
# CHECK:
# CHECK: 7897
# FIXME: handle overflowing nicely
# overflow behaviour depends on 32 vs 64 bit
# count here is isize::MAX
# we store what to print as usize, so this will overflow
# but we limit it to less than whatever the overflow is
# so this should be fine
# string repeat -m1 -n 9223372036854775807 aa
# DONTCHECK: a
# count is here (i64::MAX + 1) / 2
# we end up overflowing, and the result is 0
# but this should work fine, as we limit it way before the overflow
# string repeat -m1 -n 4611686018427387904 aaaa
# DONTCHECK: a
# Historical string repeat behavior is no newline if no output.
echo -n before
string repeat -n 5 ''
@@ -766,6 +831,18 @@ string match -qer asd asd
echo $status
# CHECK: 0
# should not be able to enable UTF mode
string match -r "(*UTF).*" "aaa"
# CHECKERR: string match: Regular expression compile error: using UTF is disabled by the application
# CHECKERR: string match: (*UTF).*
# CHECKERR: string match: ^
string replace -r "(*UTF).*" "aaa"
# CHECKERR: string replace: Regular expression compile error: using UTF is disabled by the application
# CHECKERR: string replace: (*UTF).*
# CHECKERR: string replace: ^
string match -eq asd asd
echo $status
# CHECK: 0
@@ -832,6 +909,12 @@ echo "foo1x foo2x foo3x" | string match -arg 'foo(\d)x'
echo -n abc | string upper
echo '<eol>'
# CHECK: ABC<eol>
# newline should not appear from nowhere when command does not split on newline
echo -n abc | string collect
echo '<eol>'
# CHECK: abc<eol>
printf \<
printf my-password | string replace -ra . \*
printf \>\n