summaryrefslogtreecommitdiff
path: root/pjlib/include
diff options
context:
space:
mode:
authorBenny Prijono <bennylp@teluu.com>2005-11-21 17:01:50 +0000
committerBenny Prijono <bennylp@teluu.com>2005-11-21 17:01:50 +0000
commit5721450040bd1b09af00123679577ff190da006b (patch)
tree9c330a924d04c12e180c13625dc0937e6f3d5558 /pjlib/include
parent0f263784356feca86786c2d2ade9eedf2f41a236 (diff)
Added pj_stricmp_alnum() to speedup the parser!!!
git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@72 74dad513-b988-da41-8d7b-12977e46ad98
Diffstat (limited to 'pjlib/include')
-rw-r--r--pjlib/include/pj/string.h22
-rw-r--r--pjlib/include/pj/string_i.h48
2 files changed, 63 insertions, 7 deletions
diff --git a/pjlib/include/pj/string.h b/pjlib/include/pj/string.h
index c7310b59..83f6ab73 100644
--- a/pjlib/include/pj/string.h
+++ b/pjlib/include/pj/string.h
@@ -334,10 +334,28 @@ PJ_IDECL(int) pj_strncmp2( const pj_str_t *str1, const char *str2,
*
* @return
* - < 0 if str1 is less than str2
- * - 0 if str1 is identical to str2
+ * - 0 if str1 is equal to str2
* - > 0 if str1 is greater than str2
*/
-PJ_IDECL(int) pj_stricmp( const pj_str_t *str1, const pj_str_t *str2);
+PJ_IDECL(int) pj_stricmp(const pj_str_t *str1, const pj_str_t *str2);
+
+/**
+ * Perform lowercase comparison to the strings which consists of only
+ * alnum characters. More over, it will only return non-zero if both
+ * strings are not equal, and is not able to detect which string is
+ * 'less'.
+ *
+ * If non-alnum inputs are given, then the function may mistakenly
+ * treat two strings as equal while they're not.
+ *
+ * @param str1 The string to compare.
+ * @param str2 The string to compare.
+ *
+ * @return
+ * - 0 if str1 is equal to str2
+ * - (-1) if not equal.
+ */
+PJ_IDECL(int) pj_stricmp_alnum(const pj_str_t *str1, const pj_str_t *str2);
/**
* Perform lowercase comparison to the strings.
diff --git a/pjlib/include/pj/string_i.h b/pjlib/include/pj/string_i.h
index 3fdcd2c3..f800ba84 100644
--- a/pjlib/include/pj/string_i.h
+++ b/pjlib/include/pj/string_i.h
@@ -17,6 +17,8 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
+#include <pj/pool.h>
+
PJ_IDEF(pj_str_t) pj_str(char *str)
{
pj_str_t dst;
@@ -157,13 +159,49 @@ PJ_IDEF(int) pj_strcmp2( const pj_str_t *str1, const char *str2 )
PJ_IDEF(int) pj_stricmp( const pj_str_t *str1, const pj_str_t *str2)
{
- pj_ssize_t diff;
+ register int len = str1->slen;
+ if (len != str2->slen) {
+ return (int)(len - str2->slen);
+ } else if (len == 0) {
+ return 0;
+ } else {
+ return pj_native_strnicmp(str1->ptr, str2->ptr, len);
+ }
+}
- diff = str1->slen - str2->slen;
- if (diff) {
- return (int)diff;
+PJ_IDEF(int) pj_stricmp_alnum(const pj_str_t *str1, const pj_str_t *str2)
+{
+ register int len = str1->slen;
+
+ if (len != str2->slen) {
+ return -1;
+ } else if (len == 0) {
+ return 0;
} else {
- return pj_native_strnicmp(str1->ptr, str2->ptr, str1->slen);
+ register const pj_uint32_t *p1 = (pj_uint32_t*)str1->ptr,
+ *p2 = (pj_uint32_t*)str2->ptr;
+ while (len > 3 && (*p1 & 0x1F1F1F1F)==(*p2 & 0x1F1F1F1F))
+ ++p1, ++p2, len-=4;
+
+ if (len > 3)
+ return -1;
+#if defined(PJ_IS_LITTLE_ENDIAN) && PJ_IS_LITTLE_ENDIAN!=0
+ else if (len==3)
+ return ((*p1 & 0x001F1F1F)==(*p2 & 0x001F1F1F)) ? 0 : -1;
+ else if (len==2)
+ return ((*p1 & 0x00001F1F)==(*p2 & 0x00001F1F)) ? 0 : -1;
+ else if (len==1)
+ return ((*p1 & 0x0000001F)==(*p2 & 0x0000001F)) ? 0 : -1;
+#else
+ else if (len==3)
+ return ((*p1 & 0x1F1F1F00)==(*p2 & 0x1F1F1F00)) ? 0 : -1;
+ else if (len==2)
+ return ((*p1 & 0x1F1F0000)==(*p2 & 0x1F1F0000)) ? 0 : -1;
+ else if (len==1)
+ return ((*p1 & 0x1F000000)==(*p2 & 0x1F000000)) ? 0 : -1;
+#endif
+ else
+ return 0;
}
}