rejetto forum

error in macro substring

bacter · 7 · 7786

0 Members and 1 Guest are viewing this topic.

Offline bacter

  • Operator
  • Tireless poster
  • *****
    • Posts: 681
    • View Profile
There is a error in the substring macro when the include option should include the final delimiter(2,3): instead of including the delimiter, the extracted substring is shortened.

Example:

sample string: abcd[123456]efgh
Macro: {.substring|[|]|abcd[123456]efgh|include=x.}
        -> typing error found by mars :D

Results:
no include option: [123456
with include = 0 : 123456
with include = 1 : [123456
with include = 2 : 12345      ------> should be 123456]
with include = 3 : [12345     ------> should be [123456]  


« Last Edit: October 16, 2009, 03:06:49 PM by bacter »
your computer has no brain - use your own !


Offline Mars

  • Operator
  • Tireless poster
  • *****
    • Posts: 2062
    • View Profile
{.substring|[|]abcd[123456]efgh|include=x.}

The macro is incomplete, impossible to reproduce the errors under this shape, missing a  | before abcd ;)

The bug is confirmed, the code must be modified as it to work

Quote
 procedure substring();
  var
    i, j: integer;
    s: string;
    what2inc: integer;
    caseSens: boolean;
  begin
  result:='';

  // input what to be included in the result
  s:=par('include');
  try what2inc:=strToInt(s)
  except // we also support the following values
    if s = 'none' then what2inc:=0
    else if s = 'both' then what2inc:=3
    else if s = '1+2' then what2inc:=3
    else what2inc:=1; // by default we include only the first delimiter
    end;

  caseSens:=isTrue(par('case'));

  // find the delimiters
  s:=macroDequote(par(2));
  if pars[0] = '' then i:=1
  else i:=pos_(caseSens, pars[0], s); // we don' trim this, so you can use blank-space as delimiter
  if i = 0 then exit;
  j:=pos_(caseSens, pars[1], s, i+1);
  if j = 0 then j:=length(s)+1;

  // apply what2inc
  if what2inc and 1 = 0 then
    inc(i, length(pars[0]));
  if what2inc and 2 > 0 then
    INC(j, length(pars[1]));

  // end of the story
  result:=macroQuote(copy(s, i, j-i));
  end; // substring

An error of 'common sense', certainly ;)

Just a little of work in more for rejetto  :D
« Last Edit: October 16, 2009, 12:11:06 PM by Mars »


Offline rejetto

  • Administrator
  • Tireless poster
  • *****
    • Posts: 13521
    • View Profile

Offline bacter

  • Operator
  • Tireless poster
  • *****
    • Posts: 681
    • View Profile
Thanks to mars for his effort to find in the code the bug and propose the solution, as well as having the eye to see my typing error. ;)
And thanks to rejetto for fixing this and all other bugs that are found  ;)
your computer has no brain - use your own !


Offline rejetto

  • Administrator
  • Tireless poster
  • *****
    • Posts: 13521
    • View Profile
haha, but you saved me 5 minutes pointing me directly to the  fix


Offline SilentPliz

  • Operator
  • Tireless poster
  • *****
    • Posts: 1298
  • ....... chut ! shh!
    • View Profile
Je vous remercie de voir tant de mercis... si! si!

Merci encore! ;D

Je suis sans merci ! ;D
« Last Edit: October 19, 2009, 08:46:41 AM by SilentPliz »


Offline SilentPliz

  • Operator
  • Tireless poster
  • *****
    • Posts: 1298
  • ....... chut ! shh!
    • View Profile
rejetto wrote:
Quote
Follow members gave a thank to your post:

rejetto
For this post, 1 member gave a thank you!

 ;D ;D ;D ;D excellent! ;D ;D ;D ;D ...








... Merci! :D


« Last Edit: October 19, 2009, 01:03:02 PM by SilentPliz »