Adopting Olestrings (BSTR)
This brings h2o into line with thinBasic for handling dynamic strings, ensuring that strings are consistently allocated and deallocated by the OS.
This code exposes the raw mid$() function. A negative index is read as counting from the end of the string backwards instead of counting from the beginning of the string forwards.
[code=thinbasic]
uses "OXYGEN"
dim src as string
dim s,t as string
dim v as long
src= "
push ebp
mov ebp,esp
push ebx
mov eax,[ebp+8]
mov eax,[eax] ' deref once
mov ebx,kernel
mid eax,[ebp+12],[ebp+16]
pop ebx
mov esp,ebp
pop ebp
ret 12
"
o2_buf 1 : o2_asmo src : declare function MIDS ( s as string, byval i as long, byval le as long ) as string at o2_buf 1
if len(o2_error) then msgbox 0,o2_error()+o2_view (src) : stop
S="one two three"
t=mids s,5,3
't=mids s,-5,5
msgbox 0,s+$cr+t
[/code]
Internal: (FreeBasic)
[code=thinbasic]
function mids(byval p as BSTR,byval i as long,byval j as long) as BSTR
asm
mov edi,[p] 'pointer
mov edx,[edi-4] 'length
'-------------'neg offsets
mov esi,[i]
cmp esi,0
jg midok2 'skip if positive
add esi,edx 'adjust negs
inc esi
cmp esi,1 'lowest limit=1
jge midok1
mov esi,1 'minimum is 1
midok1:
mov [i],esi
midok2:
mov esi,edi 'pointer
cmp edx,[i]
jge midok3 'past end?
push 0
push 0
call SysAllocStringByteLen
mov esi,eax
jmp midx 'null allocation
midok3:
add edi,[i] 'src base
dec edi 'base adjust
mov eax,edi 'src base
add eax,[j] 'req boundary
add edx,esi 'end boundary
cmp eax,edx
jle midok4 'choose lower length
mov eax,edx
midok4:
sub eax,edi 'set length
push eax 'bytes
push eax '
push 0 'null string
call SysAllocStringByteLen
push edi 'src base
push eax 'new string dest
mov esi,eax
call copyn
midx:
mov [function],esi ' new string
end asm
end function[/code]
Bookmarks