You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'd really like to see a way to do basic string manipulation of variables, similar to how bash provides substring offsets, pruning of prefix/suffix patterns, and even regular expressions. Having to spawn a separate program just to dice up one string is rather slow and awkward.
Reacted by Alexey Alekhin, jackblades, Bruno Duyé, Elliott Beach, Théo Biasutto--Lervat, Mathieu PATUREL, Alireza |S.N| and Jan Christoph Uhde
+1 for this. There seems to be even no way to test whether a string matches a wildcard except using switch ... case. Tcl's string builtin (doc) might be of reference value.
Yes, please implement regular expression matching! It will make working with them so much easier! Right now you have to use grep, which is kind of awkward for simple use cases like "does this string start with a number?".
The main challenge here is finding a nice syntax that is compatible with fish's command-argument format. It would be nice if we could import a command set wholesale from some other language (Tcl? Lua? Windows PowerShell?). Suggestions are very welcome.
A possible solution is to implement something like member access operator (like ':' from lua). It can by only syntactic sugar like this:
'some string'.split(args)
to call
(split 'some string' args).
Some thoughts about it:
It's simple and allows to use not only builtin string manipulation functions but dozens of installed applications.
It can work with strings, variables, arrays, command output
two\ words.split()
$variable[1].split()
$array.split() # $array[0].split() array[1].split() ...
(ls).join().match('pattern')
split command is also accessible as builtin:
split 'some long string' | grep long
The problem again is to find some nice character which will not be ambiguous with current syntax.
echo'some string'| fish_split ''# or cleave, but...
Something like that, perhaps (also, perhaps involving <<< instead of piping echo, but I don't think that matters much).
Also, 'some string'.split(args) doesn't look like shell language at all. Besides, while this case is mostly unambiguous, what would you do about (some program).split. I'm sure that .split could be a file extension, or something.
@glitchmr '.' was used only as example and can not be used in real life. I've pointed out that some other character(s) should be used to avoid ambiguity.
I like member-acees like syntax, because it allows to modify the commandline without moving cursor back and forward to put a brace: somecommand 'some very long string'
is much easier to modify to somecommand 'some very long string'.split()
or somecommand 'some very long string'@split()
than to somecommand (echo 'some very long string' | split)
I think that just make things complicated. I agree more on the Tcl alike proposal.
The pipe and new syntax and be avoided, because string is just a builtin function
The behavior I've described can be also achieved by special function 'apply': somecommand 'some very long string'(apply split)
causing 'apply' to get the string just before the command substitution and passing as argument to split, as if one calls: somecommand (split 'some very long string')
In this approach 'apply' can be aware about arrays and can call more complex commands by choosing a place, where the argument can be put: somecommand 'some very long string'(apply split \$argv) somecommand $array(apply -a join \$argv) # to swallow the whole array.
apply is awful hack that simply couldn't work (it does stuff that user generated functions simply cannot do). If we do that, why not go with something like (builtin join ":" $PATH).
@glitchmr, because this way doesn't require moving cursor back and forth to insert brackets, pipes and other stuff and allows simple token manipulation: you need to add the code only from the one side of a token, but no to wrap it.
I personally find somecommand (echo $something | split) painful.
apply, if it's added would not be the user generated function, but a builtin, and it's not more the hack then and, or and not builtins that are parsed in a special way as well.
Other than and parsing stuff like begin or if, I don't see it being parsed specifically. It just make the thing after it a command. However, your proposed apply would read thing before command itself.
'and' and commands like this affect the color of the next token (check 'and echo' and 'sudo echo'), that is done internally when parsing the command line. In some sense it's a hack, but useful one.
And what is the problem to read the token before the command substitution if it will simplify the scripting while keeping the syntax clean?
Go for it. We should measure the binary size before and after though and make sure that we're getting stripping and dead code elimination like we expect.
After adding the pcre2 calls to do regex string matching, plus the code for the other string subcommands, the increase in the size of the fish binary is 178KB (x86/64). Is that okay?
Does fish make any assumptions about sizeof(wchar_t)? I'm looking at whether I need to make the pcre2 calls work with both 16-bit and 32-bit wide characters.
Regex string match is working, leaving only regex string replace to be done.
A couple outstanding questions:
What should string split do with an empty separator? Possibilities: do no splitting, split individual characters, or fail. Currently it does no splitting, but I'm inclined to change it to split individual characters.
In configure.ac there is a check for regex.h but that header is not used anywhere. Should I remove the check?
Activity
xiaq commentedon Dec 14, 2012
+1 for this. There seems to be even no way to test whether a string matches a wildcard except using
switch ... case. Tcl'sstringbuiltin (doc) might be of reference value.MaskRay commentedon Dec 14, 2012
+1. test(1) in coreutils can not handle glob and regex. Fish needs some string manipulation functions.
merlinthered commentedon Mar 21, 2013
Yes, please implement regular expression matching! It will make working with them so much easier! Right now you have to use grep, which is kind of awkward for simple use cases like "does this string start with a number?".
fkeet commentedon Jul 25, 2013
+1
leafonsword commentedon Jul 26, 2013
+1
ridiculousfish commentedon Jul 26, 2013
The main challenge here is finding a nice syntax that is compatible with fish's command-argument format. It would be nice if we could import a command set wholesale from some other language (Tcl? Lua? Windows PowerShell?). Suggestions are very welcome.
maxfl commentedon Jul 26, 2013
A possible solution is to implement something like member access operator (like ':' from lua). It can by only syntactic sugar like this:
'some string'.split(args)
to call
(split 'some string' args).
Some thoughts about it:
two\ words.split()
$variable[1].split()
$array.split() # $array[0].split() array[1].split() ...
(ls).join().match('pattern')
split 'some long string' | grep long
The problem again is to find some nice character which will not be ambiguous with current syntax.
sugar700 commentedon Jul 26, 2013
Something like that, perhaps (also, perhaps involving
<<<instead of piping echo, but I don't think that matters much).Also,
'some string'.split(args)doesn't look like shell language at all. Besides, while this case is mostly unambiguous, what would you do about(some program).split. I'm sure that.splitcould be a file extension, or something.maxfl commentedon Jul 26, 2013
@glitchmr '.' was used only as example and can not be used in real life. I've pointed out that some other character(s) should be used to avoid ambiguity.
I like member-acees like syntax, because it allows to modify the commandline without moving cursor back and forward to put a brace:
somecommand 'some very long string'is much easier to modify to
somecommand 'some very long string'.split()or
somecommand 'some very long string'@split()than to
somecommand (echo 'some very long string' | split)wwwjfy commentedon Jul 26, 2013
I think that just make things complicated. I agree more on the Tcl alike proposal.
The pipe and new syntax and be avoided, because string is just a builtin function
maxfl commentedon Jul 26, 2013
The behavior I've described can be also achieved by special function 'apply':
somecommand 'some very long string'(apply split)causing 'apply' to get the string just before the command substitution and passing as argument to split, as if one calls:
somecommand (split 'some very long string')In this approach 'apply' can be aware about arrays and can call more complex commands by choosing a place, where the argument can be put:
somecommand 'some very long string'(apply split \$argv)somecommand $array(apply -a join \$argv)# to swallow the whole array.sugar700 commentedon Jul 26, 2013
applyis awful hack that simply couldn't work (it does stuff that user generated functions simply cannot do). If we do that, why not go with something like(builtin join ":" $PATH).maxfl commentedon Jul 26, 2013
@glitchmr, because this way doesn't require moving cursor back and forth to insert brackets, pipes and other stuff and allows simple token manipulation: you need to add the code only from the one side of a token, but no to wrap it.
I personally find
somecommand (echo $something | split)painful.apply, if it's added would not be the user generated function, but a builtin, and it's not more the hack thenand,orandnotbuiltins that are parsed in a special way as well.sugar700 commentedon Jul 26, 2013
Other than
andparsing stuff likebeginorif, I don't see it being parsed specifically. It just make the thing after it a command. However, your proposedapplywould read thing before command itself.maxfl commentedon Jul 26, 2013
'and' and commands like this affect the color of the next token (check 'and echo' and 'sudo echo'), that is done internally when parsing the command line. In some sense it's a hack, but useful one.
And what is the problem to read the token before the command substitution if it will simplify the scripting while keeping the syntax clean?
71 remaining items
msteed commentedon Jul 31, 2015
I am almost ready to start on the regex functionality. Any objection to using pcre2 then?
ridiculousfish commentedon Jul 31, 2015
Go for it. We should measure the binary size before and after though and make sure that we're getting stripping and dead code elimination like we expect.
msteed commentedon Aug 2, 2015
After adding the pcre2 calls to do regex string matching, plus the code for the other string subcommands, the increase in the size of the fish binary is 178KB (x86/64). Is that okay?
ridiculousfish commentedon Aug 2, 2015
Yes, seems OK. I and others can put it on a diet 😀 Thank you for measuring!
msteed commentedon Aug 4, 2015
Does fish make any assumptions about
sizeof(wchar_t)? I'm looking at whether I need to make the pcre2 calls work with both 16-bit and 32-bit wide characters.ridiculousfish commentedon Aug 4, 2015
fish assumes wchar_t is 32 bits (*nix) or 16 bits (Windows).
msteed commentedon Aug 8, 2015
Regex
string matchis working, leaving only regexstring replaceto be done.A couple outstanding questions:
string splitdo with an empty separator? Possibilities: do no splitting, split individual characters, or fail. Currently it does no splitting, but I'm inclined to change it to split individual characters.configure.acthere is a check forregex.hbut that header is not used anywhere. Should I remove the check?ridiculousfish commentedon Aug 9, 2015
faho commentedon Oct 4, 2015
This has been fixed via #2296, no?
ridiculousfish commentedon Oct 4, 2015
woohoo!