#include "crt/string.bi"
function str_len(byval src as zstring ptr)as uinteger
if src = 0 then return 0
dim as uinteger i = 0
while src[i] <> 0
dim as uinteger ptr p = cast(uinteger ptr,@src[i])
if ((*p - &h01010101) and &h80808080) <> 0 then
if (hiByte(loword(*p))) = 0 then return i + 1
if (loByte(hiword(*p))) = 0 then return i + 2
return i + 3
exit while
end if
i += 4
wend
return 4 + i - 4
end function
dim as string s = "The quick brown fox jumped over the lazy dog"
print str_len(s)
print strlen(s)
print len(s)
sleep
function StrLength(s as string) as long
return *Cast(Integer Ptr,Cast(Integer,@s)+sizeof(integer))
end function
dim as string s = "The quick brown fox jumped over the lazy dog"
print StrLength(s)
sleep
function StrLength(s as string) as long
return Cast(Integer Ptr, @s)[1]
end function
But for a non var-len string argument, this function call begins by copying the to be passed string into a var-len string, and passes this last string by reference (the "s" parameter) to the function.
Last edited by fxm on Oct 15, 2017 14:06, edited 2 times in total.
That one got me. I had to read Help. I knew about array and string indexing but a pointer index had not sunk in. The section 'Indexing Operators' is, to my mind, too concise and could do with an example for each type.
With regard the 'Indexing Operators' page what I had not done was click on 'Operator [] (Pointer Index)' which got me to the 'Operator [] (Pointer Index)' page. I had not been there before were ' shorthand for "*(lhs + rhs)" ' is mentioned, as you pointed out, fxm.
I must remember that items in bold are links. This is not intuitive. When I post a link in the forum I use a blue font colour and have it underlined, clearly indicating a link.
Perhaps I am being finicky but when I create a link in HelpNDoc the default is blue underlined.
fxm wrote:becoming underlined when the mouse is on them
Yeah, but we only learn of a link when we do so. If it was already underlined then that may mean a link or just underlined text. Underlined and blue will indicate a link for sure. I often use bold text for emphasis but not for a link.
May be I am being finicky. Others may have an opinion on this. Let's hear them.
Perhaps we should have a different thread on how links should be displayed in Help - I don't want to drown the power of FB's pointers shown in the above code snippets.
function StrLength(s as string) as long
return *Cast(Integer Ptr,Cast(Integer,@s)+sizeof(integer))
end function
dim as string s = "The quick brown fox jumped over the lazy dog"
print StrLength(s)
sleep
You can access from everywhere to the descriptor address of a var-len string "s" by using "@s".
This descriptor comports 3 Integers:
(0) : Address of string's character data (same value as "StrPtr(s)")
(1) : Number of string's used characters (same value as "Len(s)")
(2) : Number of string's allocated spaces
So "Cast(Integer Ptr, @s)[1]" accesses to the string length.