Groups | Search | Server Info | Login | Register


Groups > comp.lang.awk > #9986

Re: substr() - copying or not copying, that is here the question.

From Janis Papanagnou <janis_papanagnou+ng@hotmail.com>
Newsgroups comp.lang.awk
Subject Re: substr() - copying or not copying, that is here the question.
Date 2025-06-01 15:27 +0200
Organization A noiseless patient Spider
Message-ID <101hkfn$24hd5$1@dont-email.me> (permalink)
References <101f9oo$18edp$1@dont-email.me> <101fv4s$1g5c8$1@dont-email.me> <87h60zrbea.fsf@bsb.me.uk> <101hecq$22ab2$1@dont-email.me> <101hfnk$2qrh$2@news.xmission.com>

Show all headers | View raw


On 01.06.2025 14:06, Kenny McCormack wrote:
> In article <101hecq$22ab2$1@dont-email.me>,
> Janis Papanagnou  <janis_papanagnou+ng@hotmail.com> wrote:
> ...
>>> An alternative (depending on the context) would be to consider an
>>> extension that provides an index function with a third argument giving
>>> the initial offset.  I've not looked at how extensions get access to
>>> GAWK strings, so this many not be as easy as it sounds, but I would
>>> guess that it might be relatively simple to do.
>>
>> This, first of all, sounds like a good idea! It would make it
>> unnecessary to (mis-)use the substr() function as (sort of) a
>> costly copying-descriptor.[*]
>>
>> I'm unsure about using an extension here. Would there be a name
>> clash between an built-in index(haystack,needle) function and an
>> extension index(haystack,needle,start) function? Should they be
>> separate functions in the first place? (I don't think so.)
> 
> Nobody is talking about changing the index() built-in function.

Well, I was.

asort(source [, dest [, how ] ])
gensub(regexp, replacement, how [, target])
gsub(regexp, replacement [, target])
match(string, regexp [, array])
patsplit(string, array [, fieldpat [, seps ] ])
split(string, array [, fieldsep [, seps ] ])
substr(string, start [, length ])  # already standard Awk
close(filename [, how])
mktime(datespec [, utc-flag ])
strftime([format [, timestamp [, utc-flag] ] ])

...there are already standard Awk functions (sensibly!) extended by
optional arguments, and also GNU Awk specific functions that either
have from the beginning optional arguments or got extended later.

> 
> This would be a brand new function, written as an extension library.
> 
> You could name it something like index_ex() if you like, or you could give
> it a brand new name (**).

None of that I'd find a good solution given the existence of index().
(YMMV.)

> [...]
> 
> (**) My conception of how this would be implemented would handle the
> "start"-only case (just add the offset to the "haystack" arg of strstr() -
> with error checking to make sure it doesn't overflow, of course).
> Implementing "end" would be a bit trickier (but not much).

Yes, I'd second that.

Janis

Back to comp.lang.awk | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

substr() - copying or not copying, that is here the question. Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-05-31 18:12 +0200
  Re: substr() - copying or not copying, that is here the question. mack@the-knife.org (Mack The Knife) - 2025-05-31 19:07 +0000
    Re: substr() - copying or not copying, that is here the question. Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-06-01 00:16 +0200
      Re: substr() - copying or not copying, that is here the question. Ben Bacarisse <ben@bsb.me.uk> - 2025-06-01 11:42 +0100
        Re: substr() - copying or not copying, that is here the question. Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-06-01 13:43 +0200
          Re: substr() - copying or not copying, that is here the question. gazelle@shell.xmission.com (Kenny McCormack) - 2025-06-01 12:06 +0000
            Re: substr() - copying or not copying, that is here the question. Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-06-01 15:27 +0200
        Re: substr() - copying or not copying, that is here the question. gazelle@shell.xmission.com (Kenny McCormack) - 2025-06-01 11:53 +0000
          Re: substr() - copying or not copying, that is here the question. Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-06-01 15:47 +0200
            Re: substr() - copying or not copying, that is here the question. gazelle@shell.xmission.com (Kenny McCormack) - 2025-06-01 14:17 +0000
              Re: substr() - copying or not copying, that is here the question. Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-06-08 00:01 +0200
      Re: substr() - copying or not copying, that is here the question. mack@the-knife.org (Mack The Knife) - 2025-06-03 06:56 +0000
        Re: substr() - copying or not copying, that is here the question. gazelle@shell.xmission.com (Kenny McCormack) - 2025-06-03 11:04 +0000
        Re: substr() - copying or not copying, that is here the question. Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-06-08 00:05 +0200
          Re: substr() - copying or not copying, that is here the question. mack@the-knife.org (Mack The Knife) - 2025-06-08 12:35 +0000
            Re: substr() - copying or not copying, that is here the question. Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-06-11 11:07 +0200
              Meta chat (Was: substr() - copying or not copying, that is here the question.) gazelle@shell.xmission.com (Kenny McCormack) - 2025-06-11 12:11 +0000
    Re: substr() - copying or not copying, that is here the question. Kaz Kylheku <643-408-1753@kylheku.com> - 2025-06-01 00:01 +0000

csiph-web