StdStrings

Post Reply
User avatar
DGDanforth
Posts: 1061
Joined: Tue Sep 17, 2013 1:16 am
Location: Palo Alto, California, USA
Contact:

StdStrings

Post by DGDanforth »

Folks,

I have find I have duplicated the procedure NewString in 95 modules. Until recently
I didn't have a home for that procedure. I just created that home and call the module
"StdStrings". Here is what that looks like.

Code: Select all

MODULE StdStrings;

	TYPE
		String* =	POINTER TO ARRAY OF CHAR;	
		ShortString* =	POINTER TO ARRAY OF SHORTCHAR;	

	PROCEDURE New* (IN x: ARRAY OF CHAR): String;
	VAR s: String;
	BEGIN
		NEW(s, 1 + LEN(x$));
		s^ := x$;
		RETURN s
	END New;
	
	PROCEDURE NewShort* (IN x: ARRAY OF SHORTCHAR): ShortString;
	VAR s: ShortString;
	BEGIN
		NEW(s, 1 + LEN(x$));
		s^ := x$;
		RETURN s
	END NewShort;
	
END StdStrings.


I suggest we add that module to BB as a standard release for say 1.7.1
OR
include those two procedures and String TYPE in the module Strings.

Does that functionality already exist in some BB module of which I am not aware?

Comments?
-Doug
Ivan Denisov
Posts: 1700
Joined: Tue Sep 17, 2013 12:21 am
Location: Russia

Re: StdStrings

Post by Ivan Denisov »

I never use such technique... We can design some extension with Strings methods. There is a prototype by Ivan Kuzmitski.
http://oberoncore.ru/bbcc/subs/strings/start
I think, that we can add regular expressions in this extension also and then publish here:
http://apps.blackboxframework.org/
User avatar
Josef Templ
Posts: 2047
Joined: Tue Sep 17, 2013 6:50 am

Re: StdStrings

Post by Josef Templ »

I have also found the pattern mentioned by Doug in my programs and it can
also be found in the BB sources.

However, this is a more complex topic when going into the details.

Firstly, there are more string operations that would be useful in some situations.
Examples are FindLast, Java's StringBuilder (or StringBuffer) and StringTokenizer functionality,
compare with ignore case, trimming, etc, etc.
So the first complication is where to stop with the useful extensions.

Another complication is that changing e.g. SqlDB.String to Strings.String (or StdStrings.String)
is an incompatible change. So it is not possible to replace all occurrences within 1.7.1.
It will also be hard to find all occurrences of this pattern in the existing sources.

In my code I also use an optimization: I reuse the string object for the empty string.
This saves a lot of heap allocations in some cases. It is possible because the empty string
is immutable.

- Josef
User avatar
DGDanforth
Posts: 1061
Joined: Tue Sep 17, 2013 1:16 am
Location: Palo Alto, California, USA
Contact:

Re: StdStrings

Post by DGDanforth »

Josef Templ wrote: In my code I also use an optimization: I reuse the string object for the empty string.
This saves a lot of heap allocations in some cases. It is possible because the empty string
is immutable.

- Josef
Thanks for that insight. I just added that feature to my StdStrings.
-Doug
Post Reply