Posts: 1,058
Threads: 367
Joined: Oct 2007
I have written this short macro to convert a character index (i0) in a unicode string (s) to its equivalent byte index (j), for search purposes, as for example in the case of "from" the zero-based character index, from which to start search. I would appreciate any comments, indeed in the case there exists a smart way to do this conversion.
Function
iniuni
;str s ;; unicode string to be searched
;int i0 ;; character index
int j
if i0 > 0
,j=2*i0
,str s3 s4 s5
,s3.unicode(s)
,s4.get(s3 0 j)
,s5.ansi(s4 -1)
,j=s5.len - 1
,out "%s %i" si j
ret j
Posts: 12,140
Threads: 142
Joined: Dec 2002
I cannot offer a smarter way.
Replace
j=s5.len - 1
to
j=s5.len
Function
iniuni
;/
function# $s i
;s - unicode string
;i - unicode character index
if i > 0
,str s3
,s3.unicode(s)
,i*2
,if(i>s3.len) i=s3.len
,s3.fix(i)
,s3.ansi
,ret s3.len
Posts: 1,058
Threads: 367
Joined: Oct 2007
Gintaras, many thanks for the correction. Regards.