Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2006-01

Addtions for strutils

6 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

ik

Hi List,

I wish to add 3 new string functions for strutils:
1. Check if a string is an integer (without + or - signs).
2. split a string into an array using a giving separator.
3. join an array into a string using a given separator char.

I'm attaching the patch file that was taken from the latest stable svn version.

Ido

Reply by email

Marco van de Voort
to ik
I wish to add 3 new string functions for strutils: 1. Check if a string is an integer (without + or - signs).

Hmm, is this not a bit overspecialisation ?

val(x,code);
result:=(code=0)  and (posexset(['+','-'],x)=0)

would already do. (untested from memory)

2. split a string into an array using a giving separator.

In some form already exists. (e.g. tstringlist) however that has some limitations.

However so has yours:
- separator aggregates are not supported.
- there is too much copy() ing going on.

I do agree however that such function, without tstringlists limitations is needed, but we need something more general I think.

Reply by email

L505
to Marco van de Voort
I wish to add 3 new string functions for strutils: 1. Check if a string is an integer (without + or - signs).

My suggestion:
Make your own unit and put it up on FPC contributed units or add it to one of my existing string extensions unit - I welcome it in my string projects if you want to place it there. I've done stuff like this before in StrWrap1.pas which is on SVN and in the contributed units section. StrWrap1 is not perfect the first time - it gets better as other people look at the unit and correct me or I do benchmarks with it.

Any case the code is better online than offline - for me it is too much hassle to try and get something placed in the FPC core RTL so I experiment with my own units first, and then surely if it is useful it will eventually get absorbed into the RTL.

Reply by email

ik
to Marco van de Voort: Hmm, is this not a bit overspecialisation ? val(x,code); result:=(code=0) and…
Marco van de Voort wrote 10 lines
> I wish to add 3 new string functions for strutils: > 1. Check if a string is an integer (without + or - signs). Hmm, is this not a bit overspecialisation ? val(x,code); result:=(code=0) and (posexset(['+','-'],x)=0) would already do. (untested from memory)

And why is that better ? I do a loop and see if I only have the valid chars or stop. What happen if I have a number with 20 or 50 chars based ? I only want to check if it contains a valid number chars, not to actually convert it to anything (no one promise you that you can convert it to any existed type!).

> 2. split a string into an array using a giving separator. In some form already exists. (e.g. tstringlist) however that has some limitations.

I also did a TString based function that does the same (a long time ago.. my code is based on an old Delphi and TP unit I written), but as you said TString (descendant) also have it ...

However so has yours: - separator aggregates are not supported.

Well it was made to be separated with one char, but you can add support to a string or a charset if you really want :)

- there is too much copy() ing going on.

I'm still looking for a way to know if a char exists even if the string is empty without any "Access Violation" errors... copy is the best hack I can think about, alto I thought today on a better way to do it ... maybe I'll rewrite it ...

I do agree however that such function, without tstringlists limitations is needed, but we need something more general I think.

Can you tell me what exactly is needed ? I changed this functions from an old unit I made (as I written above), and I'm using it in my IP Calc unit (that I'm still writing).
One of the reasons I wish to see it appear on FPC is because some people actually interested in this unit, and thous people are not using FPC at all, so it can be a good place to start.

Ido

Reply by email

Jonas Maebe
to ik
ik wrote 6 lines
> val(x,code); > result:=(code=0) and (posexset(['+','-'],x)=0) > > would already do. (untested from memory) And why is that better ?

Because it reuses existing code (although the above could even be made shorter by using TryStrToInt or so). Reusing existing code is pretty much always better, because it improves maintainability, reduces the chance to bugs and keeps the code size smaller.

Show quoted text 5 lines
I do a loop and see if I only have the valid chars or stop. What happen if I have a number with 20 or 50 chars based ? I only want to check if it contains a valid number chars, not to actually convert it to anything (no one promise you that you can convert it to any existed type!).

I really don't think this is something which is needed in an rtl unit.

Jonas

Reply by email

ik
to Jonas Maebe
Jonas Maebe wrote 14 lines
On 22 Jan 2006, at 11:24, ik wrote: >> val(x,code); >> result:=(code=0) and (posexset(['+','-'],x)=0) >> >> would already do. (untested from memory) > > And why is that better ? Because it reuses existing code (although the above could even be made shorter by using TryStrToInt or so). Reusing existing code is pretty much always better, because it improves maintainability, reduces the chance to bugs and keeps the code size smaller.

But wouldn't a too large "number" such as
123456789012345678901234567890 would failed when using with TryStrToInt, and will work on a loop based checking for number characters ?

And what if the string is empty ? There are so many ways to make it to fail, while actually try to see if the chars exists will not. If you really want a speed, while not to use regex and check something like ^\d+$ and get over with it ? You assuming that the only input you are getting will be a number or a char that is not a number.

Do you have any other basic ways to make it "maintainable" and fast that can also support large amount of characters ?

Show quoted text 8 lines
> I do a loop and see if I only have the valid > chars or stop. What happen if I have a number with 20 or 50 chars > based ? I only want to check if it contains a valid number chars, not > to actually convert it to anything (no one promise you that you can > convert it to any existed type!). I really don't think this is something which is needed in an rtl unit.

I think that we should have some type of a large group of useful units on FPC that come with the "compiler" like Sun does with Java. It might go to contrib etc... I just thought that strutils is a good place for it.

Jonas

Ido

Reply by email