Home » U++ Library support » U++ Core » Some 'missing' string functions
Re: Some 'missing' string functions [message #16339 is a reply to message #16334] |
Mon, 09 June 2008 10:51   |
hans
Messages: 44 Registered: March 2006 Location: Germany
|
Member |
|
|
The function has a bug, because in
const tchar *se = s + (len * sizeof(tchar)); :
the multiply with sizeof(tchar) is nonsense, pointer arithmetic
is defined to work with object size already;
And the function makes too many assumptions too, namely it accesses memory after len, which may work for String objects,
but not in general case:
A valid call may be
char* s= new char('A');
string.FindFirstOf(1, s, 0);
So I would suggest to change this function to
int AString<B>::FindFirstOf(int len, const tchar *s, int from) const
{
ASSERT(from >= 0 && from <= GetLength());
const tchar *ptr = B::Begin();
const tchar *e = End();
const tchar *se = s + len;
if(len == 1) {
tchar c1 = s[0];
for(const tchar *bs = ptr + from; bs < e; bs++) {
if(*bs == c1)
return (int)(bs - ptr);
}
return -1;
}
if(len == 2) {
tchar c1 = s[0];
tchar c2 = s[1];
for(const tchar *bs = ptr + from; bs < e; bs++) {
tchar ch = *bs;
if(ch == c1 || ch == c2)
return (int)(bs - ptr);
}
return -1;
}
if(len == 3) {
tchar c1 = s[0];
tchar c2 = s[1];
tchar c3 = s[2];
for(const tchar *bs = ptr + from; bs < e; bs++) {
tchar ch = *bs;
if(ch == c1 || ch == c2 || ch == c3)
return (int)(bs - ptr);
}
return -1;
}
if(len == 4) {
tchar c1 = s[0];
tchar c2 = s[1];
tchar c3 = s[2];
tchar c4 = s[3];
for(const tchar *bs = ptr + from; bs < e; bs++) {
tchar ch = *bs;
if(ch == c1 || ch == c2 || ch == c3 || ch == c4)
return (int)(bs - ptr);
}
return -1;
}
for(const tchar *bs = ptr + from; bs < e; bs++)
for(const tchar *ss = s; ss < se; ss++)
if(*bs == *ss)
return (int)(bs - ptr);
return -1;
}
Regards,
Hans
|
|
|
 |
|
Some 'missing' string functions
By: mdelfede on Fri, 25 January 2008 12:37
|
 |
|
Re: Some 'missing' string functions
By: mirek on Fri, 25 January 2008 23:05
|
 |
|
Re: Some 'missing' string functions
By: mirek on Fri, 25 January 2008 23:09
|
 |
|
Re: Some 'missing' string functions
By: mdelfede on Sat, 26 January 2008 14:06
|
 |
|
Re: Some 'missing' string functions
By: mirek on Sat, 26 January 2008 14:24
|
 |
|
Re: Some 'missing' string functions
By: mdelfede on Sat, 26 January 2008 14:49
|
 |
|
Re: Some 'missing' string functions
By: phirox on Fri, 06 June 2008 23:21
|
 |
|
Re: Some 'missing' string functions
By: mirek on Sat, 07 June 2008 16:12
|
 |
|
Re: Some 'missing' string functions
By: mr_ped on Mon, 09 June 2008 08:32
|
 |
|
Re: Some 'missing' string functions
By: hans on Mon, 09 June 2008 10:51
|
 |
|
Re: Some 'missing' string functions
By: mirek on Mon, 09 June 2008 14:27
|
 |
|
Re: Some 'missing' string functions
By: mirek on Mon, 09 June 2008 14:28
|
 |
|
Re: Some 'missing' string functions
By: captainc on Thu, 28 August 2008 10:13
|
 |
|
Re: Some 'missing' string functions
By: mirek on Thu, 28 August 2008 15:19
|
Goto Forum:
Current Time: Mon Aug 25 13:04:17 CEST 2025
Total time taken to generate the page: 0.09487 seconds
|