Skip to content

Want variable string manipulation #156

Closed

Description

@lilyball

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.

Activity

  1. xiaq commented on Dec 14, 2012

    @xiaq
    Contributor

    +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.

  2. MaskRay commented on Dec 14, 2012

    @MaskRay

    +1. test(1) in coreutils can not handle glob and regex. Fish needs some string manipulation functions.

  3. merlinthered commented on Mar 21, 2013

    @merlinthered

    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?".

  4. fkeet commented on Jul 25, 2013

    @fkeet

    +1

  5. leafonsword commented on Jul 26, 2013

    @leafonsword
    Contributor

    +1

  6. ridiculousfish commented on Jul 26, 2013

    @ridiculousfish
    Member

    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.

  7. maxfl commented on Jul 26, 2013

    @maxfl
    Contributor

    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:

    1. It's simple and allows to use not only builtin string manipulation functions but dozens of installed applications.
    2. 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')
    3. 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.

  8. sugar700 commented on Jul 26, 2013

    @sugar700
    Contributor
    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.

  9. maxfl commented on Jul 26, 2013

    @maxfl
    Contributor

    @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)

  10. wwwjfy commented on Jul 26, 2013

    @wwwjfy
    Contributor

    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

  11. maxfl commented on Jul 26, 2013

    @maxfl
    Contributor

    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.

  12. sugar700 commented on Jul 26, 2013

    @sugar700
    Contributor

    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).

  13. maxfl commented on Jul 26, 2013

    @maxfl
    Contributor

    @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.

  14. sugar700 commented on Jul 26, 2013

    @sugar700
    Contributor

    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.

  15. maxfl commented on Jul 26, 2013

    @maxfl
    Contributor

    '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?

  16. 71 remaining items

  17. msteed commented on Jul 31, 2015

    @msteed
    Contributor

    I am almost ready to start on the regex functionality. Any objection to using pcre2 then?

  18. ridiculousfish commented on Jul 31, 2015

    @ridiculousfish
    Member

    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.

  19. msteed commented on Aug 2, 2015

    @msteed
    Contributor

    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?

  20. ridiculousfish commented on Aug 2, 2015

    @ridiculousfish
    Member

    Yes, seems OK. I and others can put it on a diet 😀 Thank you for measuring!

  21. msteed commented on Aug 4, 2015

    @msteed
    Contributor

    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.

  22. ridiculousfish commented on Aug 4, 2015

    @ridiculousfish
    Member

    fish assumes wchar_t is 32 bits (*nix) or 16 bits (Windows).

  23. msteed commented on Aug 8, 2015

    @msteed
    Contributor

    Regex string match is working, leaving only regex string replace to be done.

    A couple outstanding questions:

    1. 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.
    2. In configure.ac there is a check for regex.h but that header is not used anywhere. Should I remove the check?
  24. ridiculousfish commented on Aug 9, 2015

    @ridiculousfish
    Member
    1. Split to all individual characters is right
    2. Yes, thanks!
  25. modified the milestones: , fish-future on Oct 4, 2015
  26. faho commented on Oct 4, 2015

    @faho
    Member

    This has been fixed via #2296, no?

  27. ridiculousfish commented on Oct 4, 2015

    @ridiculousfish
    Member

    woohoo!

  28. locked as resolved and limited conversation to collaborators on Apr 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions