Project

General

Profile

Feature #1516

substring function

Added by John Abbott over 3 years ago. Updated about 3 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
CoCoA-5 function: new
Target version:
Start date:
23 Oct 2020
Due date:
% Done:

100%

Estimated time:
0.99 h
Spent time:

Description

Proposal: a new function to extract a substring.
Syntax would be something like substr(str, start, end) where indexes start from 1 (as is usual in CoCoA-5).

The current approach is ugly (and surely inefficient, to put it politely)

sum([str[pos] | pos in start..end])

Also the current approach fails if the substring is more than 10^7 chars long because the dot-dot operator throws an error for ranges longer than 10^7.


Related issues

Related to CoCoA-5 - Bug #1514: Cocoa crashes when calling RingElemsClosed2020-10-22

History

#1 Updated by John Abbott over 3 years ago

This ought to be very easy: I am sure that C++ already has such a function.

I came across the problem while looking at #1514; I modified the test case, and wanted to call RingElemList instead of RingElems, but the "substring" extraction threw an exception because it is longer than 10^7 chars... hah! :-(

#2 Updated by John Abbott over 3 years ago

  • Related to Bug #1514: Cocoa crashes when calling RingElems added

#3 Updated by John Abbott over 3 years ago

The C++ substring function has the following interface:
  • substr(str,n) the substring starting at the n-th posn until the end
  • substr(srt,n,l) the substring starting at the n-th posn, of length l (or less if the end of the string is reached)

What I had originally thought (after seeing the CoCoA code) was for the substring fn to accept two posns and then give the substr defined by them (with unclarity about whether both endpoints are included).

Should we follow the C++ approach: i.e. start and length?

#4 Updated by Anna Maria Bigatti over 3 years ago

  • Target version changed from CoCoA-5.3.2 to CoCoA-5.4.0

#5 Updated by John Abbott over 3 years ago

  • Status changed from New to In Progress
  • % Done changed from 0 to 10

Start pos and length seem reasonable to me.

Should we also allow the version which goes to end of string? KISS: maybe later!

#6 Updated by John Abbott over 3 years ago

  • % Done changed from 10 to 50

I have written a first version (in BuiltinFunctions.C).

  • name: substring; I prefer not to use sbstr as that differs by just 1 letter from subst
  • args: string, pos-of-first-char, length; pos-of-first must be within the string, length must be non-neg

Must still write the man page.

#7 Updated by John Abbott over 3 years ago

  • Status changed from In Progress to Resolved
  • % Done changed from 50 to 80

I have written a (short) manual entry.

Should there be some tests? :-/

#8 Updated by John Abbott about 3 years ago

  • Status changed from Resolved to Closed
  • Assignee set to John Abbott
  • % Done changed from 80 to 100
  • Estimated time set to 0.99 h

Also available in: Atom PDF