Another Strlen

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
nimdays
Posts: 236
Joined: May 29, 2014 22:01
Location: West Java, Indonesia

Another Strlen

Post by nimdays »

I't not complicated.

Code: Select all

#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
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Another Strlen

Post by dodicat »

There are others lurking.

Code: Select all


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
 
deltarho[1859]
Posts: 4313
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Another Strlen

Post by deltarho[1859] »

dodicat's compiler must be worn to a frazzle with the parsing jobs he gives it. Took me a while to fathom out that return. Amazing!
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Another Strlen

Post by fxm »

Code: Select all

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.
deltarho[1859]
Posts: 4313
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Another Strlen

Post by deltarho[1859] »

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.

Thanks fxm.
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Another Strlen

Post by fxm »

As:
ptr[ n ]
is equivalent to:
*( ptr + n )
You must also read the "Pointer Arithmetic" page (first link at the bottom of the "Operator [] (Pointer Index)" page).
deltarho[1859]
Posts: 4313
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Another Strlen

Post by deltarho[1859] »

I had read the "Pointer Arithmetic" page.

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
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Another Strlen

Post by fxm »

I did not well understand where the links are in bold black and not blue (becoming underlined when the mouse is on them) ?
deltarho[1859]
Posts: 4313
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Another Strlen

Post by deltarho[1859] »

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.
deltarho[1859]
Posts: 4313
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Another Strlen

Post by deltarho[1859] »

I have just started a thread in Community Discussion.
nimdays
Posts: 236
Joined: May 29, 2014 22:01
Location: West Java, Indonesia

Re: Another Strlen

Post by nimdays »

dodicat wrote:There are others lurking.

Code: Select all


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
 
This is internal right ?
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Another Strlen

Post by fxm »

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.
Post Reply