Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Character-to-byte unicode strings index conversion
#1
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
Code:
Copy      Help
;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
#2
I cannot offer a smarter way.

Replace
j=s5.len - 1
to
j=s5.len

Function iniuni
Code:
Copy      Help
;/
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
#3
Gintaras, many thanks for the correction. Regards.


Forum Jump:


Users browsing this thread: 1 Guest(s)