Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#111 Additional string functions

closed-accepted
Ethan Merritt
None
5
2005-07-04
2005-06-01
Ethan Merritt
No

This patchset implements a substring operator, in the
form of a range specifier than may be postfixed to any
string-valued expression. It is pretty straightforward,
but I post it here for comments on the syntax.

Usage

substring = string[begin:end]

where begin and end are integer indices specifying
character position in the orginal string. As with the
existing gnuplot range specifiers, these may be empty
or '*' to indicate auto-expansion to one extreme of the
string.

Example output from .../demo/substring.dem

each line is from a print statement of the form
print "foo[x:y] =", foo[x:y]

foo = ABCDEF
foo[2:4] = CDE
foo[0:0] = A
foo[4:2] =
beg = 1 end = 3
foo[beg:end] = BCD
foo[end:beg] =
foo[4:] = EF
foo[4:*] = EF
foo[:] = ABCDEF
foo[*:*] = ABCDEF

Possible issues

The range specifier notation seems appropriate, but it
is exactly equivalent to providing a subroutine
substring(string,begin,end). Does anyone have a
preference for the subroutine form?

The indexing uses C convention; the start of the string
is foo[0:*], not foo[1:*]. This is maybe annoying to
non-C programmers, but would still be an issue if the
UI were a function rather than a range specifier.

So far as I can see, there is no conflict with any
existing use of square brackets in gnuplot syntax.
Neither of the existing forms
plot {ranges} ...
or
set ?range [...]
occurs in the context of expression evaluation,
whereas the new use can *only* occur in the context of
expression parsing. But maybe I am missing something.
Can anyone see a problem?

Discussion

  • Harald Harders
    Harald Harders
    2005-06-03

    Logged In: YES
    user_id=207272

    To get a similar notation as for the sprintf function, the
    proposed _additional_ function substring(stringvar,start,end)
    could be interesting. But, the operator should also exist.

    Also, foo[x] could be interesting to get only one character.

    May be, for non-C programmers, an operator foo(beg:end),
    starting with 1 instead of 0, could be good (in addition to foo
    [beg:end]).

     
  • Petr Mikulik
    Petr Mikulik
    2005-06-08

    Logged In: YES
    user_id=31505

    My proposals for the set of string commands:

    words(string)
    Returns number of words in given string.
    word(string, n)
    Returns n-th word from the string.
    (Note: it is like in REXX.)

    length(string) or strlen(string)
    Returns string length.
    index(string, substring) or strstr(string, substring)
    Returns position of the substring.

    For substring:
    a[0:5]
    OK for C and Python folk
    substr(string, from [,to])
    An alternative, to be used by awk et al folk.

     
  • Ethan Merritt
    Ethan Merritt
    2005-06-14

    Logged In: YES
    user_id=235620

    I've added the substring operator to cvs in both forms:
    substring(string,beg,end)
    string[beg:end]
    And also a function strlen(string)

    The 14jun2005 patch implements a function
    strstrt(string,substring) which is based on the standard C
    library function strstr(). It is not exactly the same as
    strstr() in that it returns an integer which is the offset
    of the requested substring within the full string, whereas
    strstr() returns a string pointer.
    haystack = "fooneedlebaz"
    where = strstrt( haystack, "needle")
    print where
    3
    print haystack[where:*]
    needlebaz

    I'm still thinking about the words() and word() suggestion.
    Wouldn't it need a whitespace specifier as well -- similar
    to clib's strtok() or strsep() functions?

     
  • Ethan Merritt
    Ethan Merritt
    2005-06-24

    • summary: Substring operator --> Additional string functions
     
  • Ethan Merritt
    Ethan Merritt
    2005-06-24

    Logged In: YES
    user_id=235620

    substring syntax "foo"[beg:end] and functions substr()
    strlen() strstrt() now in cvs, so the patcheset itself is
    now out of date.

    I'm moving this to the "Feature Request" category, since I
    have not yet looked into Petr's suggestion of rexx-like
    functions words("multi-word string") and word("multi-word
    string", <word_n>)

     
  • Ethan Merritt
    Ethan Merritt
    2005-07-04

    • status: open --> closed-accepted
     
  • Ethan Merritt
    Ethan Merritt
    2005-07-04

    Logged In: YES
    user_id=235620

    added functions to cvs
    words("string")
    word("string",n)