Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
lpstr problems
#1
Hi gintaras, hi all

Gintaras, i mess with some lpstr functions (my fault, not QM one of course)

The problem is that the lpstr seems not be reinitialized correctly in my code.

I'm aware that a lpstr can be set to null by lpstr s=0, but it seems to be an issue in new OS's about "nullification" (nullptr comes to mind, and seems not yet implemented in QM)

The general code i use for that is to declare an unitialized lpstr and make the used one equal it.

lpstr s="some text"
lpstr f
//code
....
//code

s=f

Is there a better way?

2.

str f.lpstr="some text"

is using this as fast as using a general lpstr? I ask that because using pointers is usually more efficient than direct str manipulation.

Thanks for your patience !!
#2
2. Bad. Never modify str member variables directly, unless you know exactly how str works. More info in QM help topic "str properties (lpstr, len, nc, flags)".

All until 2 - what is the problem? lpstr is raw pointer, like byte*, it does not manage the string. More info in QM help topic "Strings".
#3
i don't know, i must be doing stupid code.....

i try to tokenize using lpstr and not str variable, and when i launch the function several times, the
original lpstr string is not used anymore, only the tokenize one.
I don't want to use intermediate str string, and there is the problem, but i'll find my way out i hope...



Thanks again
#4
Gintaras Wrote:2. Bad. Never modify str member variables directly, unless you know exactly how str works. More info in QM help topic "str properties (lpstr, len, nc, flags)".

All until 2 - what is the problem? lpstr is raw pointer, like byte*, it does not manage the string. More info in QM help topic "Strings".

yes, i know that.

I wanted to avoid using str for the tok function and use ARRAY(lpstr) in arr argument, and i must mess there....
#5
the aim of the code is to replace a word by another in a lpstr string, which seems hard to do.

the tok function needs an two str variables to be equal to the lpstr string, one for be tokenized, mapped to a istringmap routine, then replace words in the second str copy, and
then return lpstr eqaul to second str.


i wanted to avoid that double str use, but can't...
#6
here's the code, show me how to avoid tmp1 and tmp2 usage if possible to do so

Macro Macro13
Code:
Copy      Help
lpstr s="ceci est mon test olympia venus"  <<<<both venus and olympia should be replaced by the uppercased first letter version
RemplaceMots(&s)
out s
;ceci est mon test Olympia Venus  <<<<<< wanted result

Main routine

Function RemplaceMots
Code:
Copy      Help
function $&texte

str s.getmacro("GarderCasse")   <<<< in this is replacement words mapping (venus,Venus)

ARRAY(lpstr) tmpArray
lpstr g
IStringMap m._create
m.Flags=1|2
m.AddList(s "csv")

str tmp1=texte
str tmp2=texte

tok tmp1 tmpArray -1 "" 1

for _i 0 tmpArray.len
,g=m.Get(tmpArray[_i])
,if(g)
,,tmp2.findreplace(tmpArray[_i] g)
texte=tmp2
#7
texte must be str, not lpstr, if you want to modify it. I don't see any reason why it should be lpstr.
#8
yes, i know :twisted:

but i find the need to use two str variable very bad too...

anayway, this code works as expected, so i'm sticking to it...
#9
Gintaras Wrote:texte must be str, not lpstr, if you want to modify it. I don't see any reason why it should be lpstr.


in fact the calling function uses lpstr variable....but i'll modify it to pass a str variable by reference...
#10
Macro Macro13
Code:
Copy      Help
lpstr s="ceci est mon test olympia venus"
str ss=s
RemplaceMots(&ss)
out ss

Function RemplaceMots
Code:
Copy      Help
function ~&texte

str s.getmacro("GarderCasse")

ARRAY(lpstr) tmpArray
lpstr g
IStringMap m._create
m.Flags=1|2
m.AddList(s "csv")

str tmp1=texte
;str tmp2=texte

tok tmp1 tmpArray -1 "" 1

for _i 0 tmpArray.len
,g=m.Get(tmpArray[_i])
,if(g)
,,texte.findreplace(tmpArray[_i] g)
#11
Now correct. In previous version the returned string is in invalid memory, because the memory was owned by tmp2 (str) which frees the memory when function exits.

Now fast enough?

With findreplace maybe need flag for "whole word".
#12
yes, works perfectly, and istringmap is much faster tha Icsv, although i use comma separator.

TY Gintaras.
#13
Maybe faster, maybe not.

Macro Macro2739
Code:
Copy      Help
out

str texte=
;Hi gintaras, hi all
;Gintaras, i mess with some lpstr functions (my fault, not QM one of course)
;The problem is that the lpstr seems not be reinitialized correctly in my code.
;I'm aware that a lpstr can be set to null by lpstr s=0, but it seems to be an issue in new OS's about "nullification" (nullptr comes to mind, and seems not yet implemented in QM)
;The general code i use for that is to declare an unitialized lpstr and make the used one equal it.
;lpstr s="some text"
;lpstr f
;//code
;....
;//code
;s=f
;Is there a better way?
;2.
;str f.lpstr="some text"
;is using this as fast as using a general lpstr? I ask that because using pointers is usually more efficient than direct str manipulation.
;Thanks for your patience !!

str dict=
;lpstr,Lpstr
;code,Code
;is,Island
;the T
;fast,Faster
;more,More

sub.RemplaceMotsRX dict texte
out texte


#sub RemplaceMotsRX
function $dict ~&texte

IStringMap m._create
m.Flags=1|2
m.AddList(dict "csv")

REPLACERX r.frepl=&sub.Callback_str_replacerx; r.paramr=&m
texte.replacerx("\b\w+\b" r 1)


#sub Callback_str_replacerx
function# REPLACERXCB&x

;out x.match
IStringMap& m=+x.rrx.paramr
lpstr k=m.Get(x.match)
if(k) x.match=k
#14
Thanks for the input, will use it for another project.

In the particular case, the speed bottleneck is not the replacement routine, but the fact that it will be called several times, and that is the slow part.

But this example will help many i'm sure...

just a precision though : what kind of regex does QM useS? i'd lioke to go deeper into that point, and wonder
what to search for in google (is it perl, pcre, a mix of those, something special to QM)?
#15
PCRE library, older version.
#16
thanks, will search for this than...
#17
last one : can getmacro load pairs from a sub function of search/replace macro??

/****code****/
str dict=getmacro("sub.wordforreplacement")
#sub Callback_str_replacerx
function# REPLACERXCB&x

;out x.match
IStringMap& m=+x.rrx.paramr
lpstr k=m.Get(x.match)
if(k) x.match=k

#sub wordforreplacement

;lpstr,Lpstr
;code,Code
;is,Island
;the T
;fast,Faster
;more,More

tried but no joy...
#18
Macro Macro2738
Code:
Copy      Help
str dict=sub.Dict
out dict

#sub Dict
function$

lpstr s=
;lpstr,Lpstr
;code,Code
;is,Island
;the T
;fast,Faster
;more,More
ret s
#19
lol, so easy i did not think about it....
was struggling with getopt...
ty
#20
i try to uppercase first letter of str variable

i do:

function ~&texte
texte[0]=_totupper(texte[0])

is there a better "QM" way (it must work with unicode that is why i use _totupper )?
#21
Macro Macro2739
Code:
Copy      Help
str s="šeškas"
s.ucase(0 1)
out s
#22
yes, found it out!!!


Forum Jump:


Users browsing this thread: 1 Guest(s)