Page 1 of 1

StdStrings

Posted: Fri Sep 30, 2016 12:44 am
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

Re: StdStrings

Posted: Sun Oct 02, 2016 4:49 am
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/

Re: StdStrings

Posted: Mon Oct 03, 2016 6:25 am
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

Re: StdStrings

Posted: Mon Oct 03, 2016 8:18 pm
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