Fast uByte array to String routine

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
cha0s
Site Admin
Posts: 5317
Joined: May 27, 2005 6:42
Location: Illinois
Contact:

Postby cha0s » Aug 30, 2005 1:31

btw, i figured out it must be called like this

Code: Select all

Return fb_StrAllocTempDescF( File( Start ), Length )


no pointer
mjs
Site Admin
Posts: 842
Joined: Jun 16, 2005 19:50
Location: Germany

Postby mjs » Aug 30, 2005 16:58

You're right ... I simply overlooked this ... sorry ...

Regards,
Mark
jcohen
Posts: 85
Joined: Jun 20, 2005 18:01

Array conversion

Postby jcohen » Oct 18, 2005 5:56

Ok, I'm not that hot with this function and I have ZERO experience with array conversion other then the simple "for" loops etc. But I have having a hard time understanding how this function is useful? Can someone give me an example.
jofers
Posts: 1525
Joined: May 27, 2005 17:18
Contact:

Postby jofers » Oct 18, 2005 7:45

Well, it's useful to FreeBASIC for making copies of strings. But you may not have a use for it. It's not terribly useful, but this kind of degraded into a cool challenge.

BTW, found another "method" :)

Code: Select all

MyString = *cPtr(zString Ptr, @Array(0))


Lost 65x To 67x for fb_StrAllocTempDescF on mjs's test, but second place ain't bad :D
Provoni
Posts: 347
Joined: Jan 05, 2014 12:33
Location: Belgium

Re: Fast uByte array to String routine

Postby Provoni » Mar 12, 2019 17:39

If this is off topic then feel free to move my post to a new topic.

Problem with code:

Code: Select all

screenres 800,600,32

dim as short i
dim as string s="12345"
dim linedata as ubyte ptr
linedata=strptr(s)

print fb_stralloctempdescf(linedata[i],2) 'print only the first number = ok
print valint(fb_stralloctempdescf(linedata[i],2)) 'print only the first number = not ok

sleep
fxm
Posts: 9529
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Fast uByte array to String routine

Postby fxm » Mar 12, 2019 19:39

Weird behavior because:
- 'fb_stralloctempdescf(reference, size_plus_1)' creates only a temporary string descriptor, pointing to the address of the passed reference, and defining a string length of 'size_plus_1' - 1 characters, but without overwriting by a null character the 'size_plus_1' nth element.
- 'valint()' only parses the string data from the left, skipping any white space, and returns the longest number it can read, stopping at the first non-suitable character it finds or at the termination character ('chr(0)'), but without using the number of characters in the string descriptor when exists.

Emulation of that 'fb_stralloctempdescf()' behavior:

Code: Select all

dim as string s="12345"
dim as integer length = 1

dim as string StrAllocTempDesc  '' string descriptor creation
cptr(zstring ptr ptr, @StrAllocTempDesc)[0] = strptr(s)
cptr(Integer ptr, @StrAllocTempDesc)[1] = length
cptr(Integer ptr, @StrAllocTempDesc)[2] = length + 1

print StrAllocTempDesc
print valint(StrAllocTempDesc)

dim as ubyte mem
swap mem, s[length]  '' overwriting the 'length + 1' nth character with a null character
print valint(StrAllocTempDesc)

cptr(zstring ptr ptr, @StrAllocTempDesc)[0] = 0  '' clearing string descriptor
cptr(Integer ptr, @StrAllocTempDesc)[1] = 0
cptr(Integer ptr, @StrAllocTempDesc)[2] = 0
swap mem, s[length]  '' recovering the 'length + 1' nth character

sleep

Code: Select all

1
 12345
 1

'fb_stralloctempdescf()', because normally reserved to a FB internal use only, does not verify the following rule:
Despite the use of the descriptor, an implicit NULL character (Chr(0)) is added to the end of the string, to allow passing them to functions in external libraries without making slow copies.

Note:
This implies that any non-empty string descriptor must always verifies for this inequality (between its third and second field):
    number_of_characters_allocated >= number_of_useful_characters + 1
Provoni
Posts: 347
Joined: Jan 05, 2014 12:33
Location: Belgium

Re: Fast uByte array to String routine

Postby Provoni » Mar 13, 2019 7:59

fxm wrote:
'fb_stralloctempdescf()', because normally reserved to a FB internal use only, does not verify the following rule:
Despite the use of the descriptor, an implicit NULL character (Chr(0)) is added to the end of the string, to allow passing them to functions in external libraries without making slow copies.

Thanks!

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 1 guest