FF_STRING library

Started by James Fuller, July 04, 2016, 11:15:36 AM

Previous topic - Next topic

James Fuller

Jose,
  Nope! :) Come on Jose you just penned a very enjoyable narrative on the Fb unbasic way to do things :)
I will not give up my "BASIC" way to do things, sorry.
The pool technique has worked for decades with BCX.

How to do this your way?

gsDlgInfo(DI_EXSTYLE) = FF_Remove(FF_Remain(1,FF_Parse(sLine,1),"("),"%")

James





James Fuller

Jose,
  Do you see any problems here? This might be a compromise.
James


#define unicode
#include "afx/CBstr.inc"

FUNCTION FF_Remove OVERLOAD (BYREF cbsMain AS CBSTR, BYREF cbsMatch AS CBSTR, BYREF cbsOut AS CBSTR) AS CBSTR
   IF LEN(cbsMain) = 0 OR LEN(cbsMatch) = 0 OR VARPTR(cbsOut) = NULL THEN EXIT FUNCTION
   cbsOut = cbsMain
   DIM i AS LONG
   DO
      i = INSTR(cbsOut, cbsMatch)
      IF i THEN
         cbsOut = LEFT(**cbsOut, i - 1) & MID(**cbsOut, i + LEN(cbsMatch))
         'FUNCTION = TRUE
      ENDIF
   LOOP UNTIL i = 0
   Function = cbsOut
END FUNCTION
'==============================================================================
Function FbMain() As Long
    Dim As CBStr cbs = "[]Hello[]"
    Dim As CBStr cbs1 = "[]"
    Dim As CBStr cbs2
    cbs2 =  FF_Remove(cbs,cbs1,cbs2)
    ? **cbs2
    sleep
    Function = 0
End Function
End FbMain()
End Function


James Fuller

Jose,
  Take a look at Temporary Types in the help file.
James

FUNCTION FF_Remove OVERLOAD (cbsMain AS CBSTR, cbsMatch AS CBSTR) AS CBSTR
   IF LEN(cbsMain) = 0 OR LEN(cbsMatch) = 0  THEN EXIT FUNCTION
   
   Dim As CBStr cbsOut = Type(cbsMain)
   DIM i AS LONG
   DO
      i = INSTR(cbsOut, cbsMatch)
      IF i THEN
         cbsOut = LEFT(**cbsOut, i - 1) & MID(**cbsOut, i + LEN(cbsMatch))
      ENDIF
   LOOP UNTIL i = 0
   Function = cbsOut
END FUNCTION


José Roca

> How to do this your way?

Step by step.

José Roca

> Do you see any problems here? This might be a compromise.

Why to reassign to cbs2 its own content? It is like doing x = x.

James Fuller

Jose,
  I only tried this with CBStr. As I said see Temporary Type in the help file
I added a ? "CBStr Destructor SysFreeString m_bstr" to the CBStr.inc so I coud see
the destructors called. 5 altogether. 2 before the  ? **cbs2 and 3 more after leaving the function.

James
   

#define unicode
#include "afx/CBstr.inc"
FUNCTION FF_Remove OVERLOAD (cbsMain AS CBSTR, cbsMatch AS CBSTR) AS CBSTR
   IF LEN(cbsMain) = 0 OR LEN(cbsMatch) = 0  THEN EXIT FUNCTION
   
   'This is the changed. See Temporary Types in the help file
   Dim As CBStr cbsOut = Type(cbsMain)
   
   DIM i AS LONG
   DO
      i = INSTR(cbsOut, cbsMatch)
      IF i THEN
         cbsOut = LEFT(**cbsOut, i - 1) & MID(**cbsOut, i + LEN(cbsMatch))
      ENDIF
   LOOP UNTIL i = 0
   Function = cbsOut
END FUNCTION
'==============================================================================
Function FbMain() As Long
    Dim As CBStr cbs = "[]Hello[]"
    Dim As CBStr cbs1 = "[]"
    Dim As CBStr cbs2
    cbs2 =  FF_Remove(cbs,cbs1)
    ? **cbs2
    Function = 0
End Function
FbMain()
Sleep
End


José Roca

Maybe you have find something of value. Will have to test.

José Roca

#22
Seems to work fine. Congratulations!

Can't be used to return a WSTRING, but works with CBSTR.

This is what I was looking for so long: to return a temporary value that will be freed by te compiler. In the case of a TYPE, it is the destructor of this TYPE who does the cleanup, which is the correct way of doing it.

This opens new possibilities, also for variants.

With CBSTR, a class for variants and the use of abstract methods, probably I will be able to make easy the use of COM.

The show stopper has always been the problem of freeing temporary results.

As always, the best solution only comes after all the others have been discarded.

Paul Squires

Isn't it always just such a great day when you see Jose getting excited about programming!  :)

Thanks James, I will raise a glass to toast you for this discovery. Well done indeed.
Paul Squires
PlanetSquires Software

James Fuller

It was under our noses all along and described quite well in the help file, not like Private.
I was just lucky I stumbled over it.
I really see no reason to use WSTRING's at all now. CBStr should have most/all bases covered shouldn't it?

Onward and Upward Jose :)

James

Paul Squires

I hope so. I haven't been testing all the code in the thread or playing with the CBStr like you and Jose have. I have been working on that editor. Hard for me to concentrate on too many things. Hopefully it will work with all of the permutations of operators that can act on the string. Also, if it does work well then we'll need to code a string builder class because I imagine hundreds or thousands of string concatenations will be slow. I thought that FB's STRING concatenations would be slow but turns out they are extremely fast because a STRING contains extra buffer and only gets resized when that buffer is full unlike BSTR's that have a size equal to the length of the string it holds.
Paul Squires
PlanetSquires Software

James Fuller

Paul,
  FreeBasic's String handling is the best.
Peter did some comparisons to his new string handling in Bacon.

James

James Fuller

I found another item that might be useful in the help file?
Memory Operators -> Operator Placement New

James


Marc Pons

#28
Hi ! very happy to see Jose very activ in this forum, ( I was learning c/ c++ during lasts months)

may I give my contribution to the unicode story for freebasic

some months ago, i've tried to create unicode dynamic string type ( I wanted to make a c exercice)
and i've countinued to do it in freebasic.

here is the last evolution i've done : uStringW type

it is similar to the freebasic dynamic string , created with constructor, allocating size by steps (32 when needed) , freeing automaticaly with destructor depending the scope and clearing all the remaining allocated when program exits.
the uStringW type is intended to work in windows mode (utf16 internally)  or linux mode (utf32)
for the windows mode, the surrogate pairs have been take into account,
some functions to manipilate the uStringW type are also included , mid, instr, left, right, len, reverse, replace...

the attached rar file includes the the source file as : Dyn_Wstring.bi
and some example files to test
plus 4 files ( various utf format ) to play with

Your comments are welcome  ;)
Marc

updated attachment

James Fuller

Quote from: Marc Pons on July 07, 2016, 08:59:17 AM
the attached rar file includes the the source file as : Dyn_Wstring.bi
and some example files to test
plus 4 files ( various utf format ) to play with

Your comments are welcome  ;)
Marc

Afraid not Marc. No Dyn_Wstring.bi in the attachment.

James