diff options
author | h7x4 <h7x4@nani.wtf> | 2022-12-10 13:02:40 +0100 |
---|---|---|
committer | h7x4 <h7x4@nani.wtf> | 2022-12-10 13:16:45 +0100 |
commit | 62e863e98c8f5c22c0dd4ee3845613a858262b4c (patch) | |
tree | c2a04ea3dc037fd9661e564b701a744070b23fe0 /lib/strings.nix | |
parent | 2bb2eec81373190a3f2de82aaa81beab3f08b649 (diff) | |
download | nixpkgs-62e863e98c8f5c22c0dd4ee3845613a858262b4c.tar nixpkgs-62e863e98c8f5c22c0dd4ee3845613a858262b4c.tar.gz nixpkgs-62e863e98c8f5c22c0dd4ee3845613a858262b4c.tar.bz2 nixpkgs-62e863e98c8f5c22c0dd4ee3845613a858262b4c.tar.lz nixpkgs-62e863e98c8f5c22c0dd4ee3845613a858262b4c.tar.xz nixpkgs-62e863e98c8f5c22c0dd4ee3845613a858262b4c.tar.zst nixpkgs-62e863e98c8f5c22c0dd4ee3845613a858262b4c.zip |
lib.strings: fix negative number handling for `toInt` and `toIntBase10`
The previous version would be unstable due to an input validation regex not expecting a '-' in front of the number.
Diffstat (limited to 'lib/strings.nix')
-rw-r--r-- | lib/strings.nix | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/lib/strings.nix b/lib/strings.nix index b5f5a4d9060..a5c3965f8f1 100644 --- a/lib/strings.nix +++ b/lib/strings.nix @@ -807,9 +807,9 @@ rec { */ toInt = str: let - # RegEx: Match any leading whitespace, then any digits, and finally match any trailing - # whitespace. - strippedInput = match "[[:space:]]*([[:digit:]]+)[[:space:]]*" str; + # RegEx: Match any leading whitespace, possibly a '-', one or more digits, + # and finally match any trailing whitespace. + strippedInput = match "[[:space:]]*(-?[[:digit:]]+)[[:space:]]*" str; # RegEx: Match a leading '0' then one or more digits. isLeadingZero = match "0[[:digit:]]+" (head strippedInput) == []; @@ -858,9 +858,10 @@ rec { */ toIntBase10 = str: let - # RegEx: Match any leading whitespace, then match any zero padding, capture any remaining - # digits after that, and finally match any trailing whitespace. - strippedInput = match "[[:space:]]*0*([[:digit:]]+)[[:space:]]*" str; + # RegEx: Match any leading whitespace, then match any zero padding, + # capture possibly a '-' followed by one or more digits, + # and finally match any trailing whitespace. + strippedInput = match "[[:space:]]*0*(-?[[:digit:]]+)[[:space:]]*" str; # RegEx: Match at least one '0'. isZero = match "0+" (head strippedInput) == []; |