Code: Select all
DIM ReturnLine AS ZSTRING * 78
ReturnLine = MID$(MyString$, Start%, Length%)
Code: Select all
DIM ReturnLine AS ZSTRING * 78
ReturnLine = MID$(MyString$, Start%, Length%)
Code: Select all
/*
* str_mid.c -- mid$ function
*
* chng: oct/2004 written [v1ctor]
*
*/
#include <stddef.h>
#include <string.h>
#include "fb.h"
/*:::::*/
FBCALL FBSTRING *fb_StrMid ( FBSTRING *src, int start, int len )
{
FBSTRING *dst;
int src_len;
FB_STRLOCK();
if( (src != NULL) && (src->data != NULL) && (FB_STRSIZE( src ) > 0) )
{
src_len = FB_STRSIZE( src );
if( (start > 0) && (start <= src_len) && (len != 0) )
{
--start;
if( len < 0 )
len = src_len;
if( start + len > src_len )
len = src_len - start;
/* alloc temp string */
dst = fb_hStrAllocTemp_NoLock( NULL, len );
if( dst != NULL )
{
FB_MEMCPY( dst->data, src->data + start, len );
/* null term */
dst->data[len] = '\0';
}
else
dst = &__fb_ctx.null_desc;
}
else
dst = &__fb_ctx.null_desc;
}
else
dst = &__fb_ctx.null_desc;
/* del if temp */
fb_hStrDelTemp_NoLock( src );
FB_STRUNLOCK();
return dst;
}
Code: Select all
function pFastMid(ps as const string ptr, start as integer, length as integer) as const string ptr
static a(0 to 3-1) as integer
a(0) = cint(strptr(*ps) + start-1)
a(1) = length
a(2) = length
dim p as const string ptr = cptr(string ptr, @a(0))
assert( sizeof(a(0)) * 3 = sizeof(string) ) '' a() same size as string?
assert( strptr(*p) = a(0) ) '' a(0) points to string data?
assert( len(*p) = a(1) ) '' a(1) is the string length?
assert( *p = mid(*ps, start, length) ) '' return matches standard mid()?
return p
end function
dim ReturnLine as zstring * 78
dim s as string = "Hello world"
ReturnLine = *pFastMid(@s, 4, 5)
write ReturnLine
if ReturnLine = "lo wo" then
print "Success"
end if
Code: Select all
dim ReturnLine as zstring * 78
dim s as string = "Hello world"
DIM p1 AS UBYTE PTR = @ReturnLine
DIM p2 AS UBYTE PTR = STRPTR(s)
memcpy p1, p2 + 3, 5
p1[5] = 0
print ReturnLine
print len(ReturnLine)
memcpy p1, p2 + 2, 2
p1[2] = 0
print ReturnLine
print len(ReturnLine)
Code: Select all
#macro memcpy(dest,src,size)
for n as long=0 to (size)/sizeof(*(src))-1
(dest)[n]=(src)[n]
next
#endmacro
dim ReturnLine as zstring * 78
dim s as string = "Hello world"
DIM p1 AS UBYTE PTR = @ReturnLine
DIM p2 AS UBYTE PTR = STRPTR(s)
memcpy (p1, p2 + 3, 5) '<--------- use brackets
p1[5] = 0
print ReturnLine
print len(ReturnLine)
memcpy (p1, p2 + 2, 2)
p1[2] = 0
print ReturnLine
print len(ReturnLine)
sleep