d****p 发帖数: 685 | 1 一个比较两个字符串的函数(相等返回0,否则-1或者1):
int compareStr(str1, str2)
if (length(str1) < length(str2))
return -1;
else if (length(str1) > length(str2))
return 1;
// two string equal in length
for each character c1i in str1 and c2i in str2
if c1i is upper case
convert c1i to lower case
if c2i is upper case
convert c2i to lower case
if (c1i < c2i)
|
y****e 发帖数: 23939 | 2 直接用STL的equal呢?
inline bool eq_nocase(char c1, char c2) {
return toupper(c1) == toupper(c2);
}
const int N = strlen(str1);
equal(str1, str1+N, str2, eq_nocase); |
X****r 发帖数: 3557 | 3 为什么不用strcasecmp?
【在 d****p 的大作中提到】 : 一个比较两个字符串的函数(相等返回0,否则-1或者1): : int compareStr(str1, str2) : if (length(str1) < length(str2)) : return -1; : else if (length(str1) > length(str2)) : return 1; : // two string equal in length : for each character c1i in str1 and c2i in str2 : if c1i is upper case : convert c1i to lower case
|
d****p 发帖数: 685 | 4 The string comes as wstring, not string.
Do you see better algorithm? The function is called so many times since it
is used to compare keys in a lot of maps.
【在 X****r 的大作中提到】 : 为什么不用strcasecmp?
|
d****p 发帖数: 685 | 5 Thanks.
I guess this function is more general purposed and should be slower than the
code posted? Anyway I will do a profiling.
【在 y****e 的大作中提到】 : 直接用STL的equal呢? : inline bool eq_nocase(char c1, char c2) { : return toupper(c1) == toupper(c2); : } : const int N = strlen(str1); : equal(str1, str1+N, str2, eq_nocase);
|
g*****g 发帖数: 34805 | 6 http://www.docjar.com/html/api/java/lang/String.java.html
你可以看看这个源码。
【在 d****p 的大作中提到】 : 一个比较两个字符串的函数(相等返回0,否则-1或者1): : int compareStr(str1, str2) : if (length(str1) < length(str2)) : return -1; : else if (length(str1) > length(str2)) : return 1; : // two string equal in length : for each character c1i in str1 and c2i in str2 : if c1i is upper case : convert c1i to lower case
|
X****r 发帖数: 3557 | 7 wcscasecmp() if available
You can check the source code of strcasecmp to get the idea. However, it
really depends on the representation of the string, e.g. checking the
length first is not efficient for C strings, but it is for C++'s
std::string.
it
【在 d****p 的大作中提到】 : The string comes as wstring, not string. : Do you see better algorithm? The function is called so many times since it : is used to compare keys in a lot of maps.
|
d****p 发帖数: 685 | |
d****p 发帖数: 685 | 9 Will try wcscasecmp - not sure it is available on all platforms.
If it doesn't work, I may consider building a lookup table to do the case
translation directly.
【在 X****r 的大作中提到】 : wcscasecmp() if available : You can check the source code of strcasecmp to get the idea. However, it : really depends on the representation of the string, e.g. checking the : length first is not efficient for C strings, but it is for C++'s : std::string. : : it
|
y***d 发帖数: 2330 | 10 不能预先都转化为 upper case 的么?
【在 d****p 的大作中提到】 : The string comes as wstring, not string. : Do you see better algorithm? The function is called so many times since it : is used to compare keys in a lot of maps.
|
|
|
d****p 发帖数: 685 | 11 That's a good idea. However for now it is not realistic since project is
close to ending and such a change may be risky - too much code relying on
this little function. I would like to say if we were starting from scratch
again, this kind of preprocessing is absolutely necessary.
I tried using a char map to convert upper char to lower case char. Scaled
cpu time spent on the function dropped from 22 to 16, around 27% boost. ie
if (c >= L'A' && c < L'Z') c -= L'A' - L'a'
to
c = charmap[c] where ch
【在 y***d 的大作中提到】 : 不能预先都转化为 upper case 的么?
|
v*s 发帖数: 946 | 12 如果是C /C++, 前面2个strlen应该去掉,合并到后面的循环中。
具体细节自己考虑吧,或者google c strcmp 函数。
【在 d****p 的大作中提到】 : 一个比较两个字符串的函数(相等返回0,否则-1或者1): : int compareStr(str1, str2) : if (length(str1) < length(str2)) : return -1; : else if (length(str1) > length(str2)) : return 1; : // two string equal in length : for each character c1i in str1 and c2i in str2 : if c1i is upper case : convert c1i to lower case
|
r****t 发帖数: 10904 | 13 有些标点符号比 upper case 大,所以不行。
【在 y***d 的大作中提到】 : 不能预先都转化为 upper case 的么?
|
r****t 发帖数: 10904 | 14 转成 lower case 以后,wstring 有 operator- 没有?如果没有的话做成 union
{wstring, long long...},或者 cast 成足够大的整数,然后用整数相减行不行呢?或者
derive wstring 实现减法也行。
is
on
scratch
Scaled
boost.
ie
under
【在 d****p 的大作中提到】 : That's a good idea. However for now it is not realistic since project is : close to ending and such a change may be risky - too much code relying on : this little function. I would like to say if we were starting from scratch : again, this kind of preprocessing is absolutely necessary. : I tried using a char map to convert upper char to lower case char. Scaled : cpu time spent on the function dropped from 22 to 16, around 27% boost. ie : if (c >= L'A' && c < L'Z') c -= L'A' - L'a' : to : c = charmap[c] where ch
|
d****p 发帖数: 685 | 15 casting wstring to large integer is a very interesting concept.thanks.
?或者
【在 r****t 的大作中提到】 : 转成 lower case 以后,wstring 有 operator- 没有?如果没有的话做成 union : {wstring, long long...},或者 cast 成足够大的整数,然后用整数相减行不行呢?或者 : derive wstring 实现减法也行。 : : is : on : scratch : Scaled : boost. : ie
|