Loading ...
Sorry, an error occurred while loading the content.

RE: "It would be nice" (Was: patch: ':let a="+10"' etc)

Expand Messages
  • Michael Geddes
    I think a new function is in order. strpartright( abc ,1) would return ab strpartright( abc ,0,1) would return c in other words if we had a function
    Message 1 of 7 , Feb 1, 2001
    • 0 Attachment
      I think a new function is in order.

      strpartright("abc",1)
      would return 'ab'
      strpartright("abc",0,1)
      would return 'c'

      in other words if we had a function reverse() that reverses a string, then
      strpartright would be
      reverse(strpart(reverse( {string}) , {arg1}[, {arg2}]


      I also think that a better 'modification' to strpart using -ve values would
      be:

      strpart( {strexpr}, {start}, -{len} )
      equivalent to
      strpart( {strexpr}, {start}, strlen({strexpr}) - {len} )

      so

      let a = "'my string'"
      echo strpart( a, 1,-1)

      would echo:
      my string


      //.

      -----Original Message-----
      From: Neil Bird [SMTP:neil.bird@...]
      Sent: Friday, 2 February 2001 2:19
      To: benji@...
      Cc: Vim-Dev Mailing List
      Subject: Re: "It would be nice" (Was: patch: ':let a="+10"'
      etc)

      Benji Fisher wrote:
      > 1. strpart("abc", 1) strips off the first character, returning
      "bc".
      > IWBNI strpart("abc", -1) stripped off the last character,
      returning "ab".

      Sounds easy enough. The only problem would be that it'd not be
      backwards
      compatible, as that first number being negative already has a
      meaning, so
      in context, it already does the right thing, IYSWIM.
    • Neil Bird
      ... In that case, it can come down to: which interpretation do people (read: Bram) prefer? I think I d vote for the new way, but I can see how correctly
      Message 2 of 7 , Feb 2, 2001
      • 0 Attachment
        Benji Fisher wrote:
        > Until vim 6.0, strpart("abc", -1) was illegal, as was
        > strpart("abc",1). Thus the compatibility issue is not too great: how
        > many scripts have been written since 6.0 alphas started coming out that
        > rely on strpart("abc",-1) returning "abc"?

        In that case, it can come down to: which interpretation do people (read:
        Bram) prefer? I think I'd vote for the 'new' way, but I can see how
        'correctly' interpreting negative start indices would be useful in scripts
        (to catch boundary cases).

        Unless, of course, another combination can be thought of. Negative
        lengths?

        --
        =====================- http://www.thalesgroup.com/ -=====================
        Neil Bird | If this .signature |
        work mailto:neil.bird@... | looks pants, then | $> cd /pub
        personal mailto:neil@... | stop using Outlook! | $> more beer
      • Bram Moolenaar
        ... I like that better than a negative argument. If the offset argument is a variable, and it accidentally becomes negative (e.g., -1), you don t suddenly
        Message 3 of 7 , Feb 2, 2001
        • 0 Attachment
          Michael Geddes wrote:

          > I think a new function is in order.
          >
          > strpartright("abc",1)
          > would return 'ab'
          > strpartright("abc",0,1)
          > would return 'c'

          I like that better than a negative argument. If the "offset" argument is a
          variable, and it accidentally becomes negative (e.g., -1), you don't suddenly
          want to get characters from the other side of the string.

          > I also think that a better 'modification' to strpart using -ve values would
          > be:
          >
          > strpart( {strexpr}, {start}, -{len} )
          > equivalent to
          > strpart( {strexpr}, {start}, strlen({strexpr}) - {len} )
          >
          > so
          >
          > let a = "'my string'"
          > echo strpart( a, 1,-1)
          >
          > would echo:
          > my string

          That makes sense. Although I think the example should be:

          echo strpart(a, 1, -2)

          You're taking off two characters, one on both sides.

          However, the when the length is computed, and you accidentally end up
          computing a negative length, you don't get what you want.

          I don't like the idea of a negative value making the function do something
          different. The meaning of the function call becomes less obvious, you have to
          look carefully to see what it does. So, let's just use that strlen()
          explicit, that's much clearer.

          For all these theoretical items, it's very useful to see some actual code that
          uses it. Only then can we find out what is easy to use and what isn't.

          --
          hundred-and-one symptoms of being an internet addict:
          120. You ask a friend, "What's that big shiny thing?" He says, "It's the sun."

          /// Bram Moolenaar -- Bram@... -- http://www.moolenaar.net \\\
          ((( Creator of Vim - http://www.vim.org -- ftp://ftp.vim.org/pub/vim )))
          \\\ Help me helping AIDS orphans in Uganda - http://iccf-holland.org ///
        Your message has been successfully submitted and would be delivered to recipients shortly.