andre@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: #include "plstr.h" andre@0: #include andre@0: andre@0: static const unsigned char uc[] = andre@0: { andre@0: '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007', andre@0: '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017', andre@0: '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027', andre@0: '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037', andre@0: ' ', '!', '"', '#', '$', '%', '&', '\'', andre@0: '(', ')', '*', '+', ',', '-', '.', '/', andre@0: '0', '1', '2', '3', '4', '5', '6', '7', andre@0: '8', '9', ':', ';', '<', '=', '>', '?', andre@0: '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', andre@0: 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', andre@0: 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', andre@0: 'X', 'Y', 'Z', '[', '\\', ']', '^', '_', andre@0: '`', 'A', 'B', 'C', 'D', 'E', 'F', 'G', andre@0: 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', andre@0: 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', andre@0: 'X', 'Y', 'Z', '{', '|', '}', '~', '\177', andre@0: 0200, 0201, 0202, 0203, 0204, 0205, 0206, 0207, andre@0: 0210, 0211, 0212, 0213, 0214, 0215, 0216, 0217, andre@0: 0220, 0221, 0222, 0223, 0224, 0225, 0226, 0227, andre@0: 0230, 0231, 0232, 0233, 0234, 0235, 0236, 0237, andre@0: 0240, 0241, 0242, 0243, 0244, 0245, 0246, 0247, andre@0: 0250, 0251, 0252, 0253, 0254, 0255, 0256, 0257, andre@0: 0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267, andre@0: 0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277, andre@0: 0300, 0301, 0302, 0303, 0304, 0305, 0306, 0307, andre@0: 0310, 0311, 0312, 0313, 0314, 0315, 0316, 0317, andre@0: 0320, 0321, 0322, 0323, 0324, 0325, 0326, 0327, andre@0: 0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337, andre@0: 0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347, andre@0: 0350, 0351, 0352, 0353, 0354, 0355, 0356, 0357, andre@0: 0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367, andre@0: 0370, 0371, 0372, 0373, 0374, 0375, 0376, 0377 andre@0: }; andre@0: andre@0: PR_IMPLEMENT(PRIntn) andre@0: PL_strcasecmp(const char *a, const char *b) andre@0: { andre@0: const unsigned char *ua = (const unsigned char *)a; andre@0: const unsigned char *ub = (const unsigned char *)b; andre@0: andre@0: if( ((const char *)0 == a) || (const char *)0 == b ) andre@0: return (PRIntn)(a-b); andre@0: andre@0: while( (uc[*ua] == uc[*ub]) && ('\0' != *a) ) andre@0: { andre@0: a++; andre@0: ua++; andre@0: ub++; andre@0: } andre@0: andre@0: return (PRIntn)(uc[*ua] - uc[*ub]); andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRIntn) andre@0: PL_strncasecmp(const char *a, const char *b, PRUint32 max) andre@0: { andre@0: const unsigned char *ua = (const unsigned char *)a; andre@0: const unsigned char *ub = (const unsigned char *)b; andre@0: andre@0: if( ((const char *)0 == a) || (const char *)0 == b ) andre@0: return (PRIntn)(a-b); andre@0: andre@0: while( max && (uc[*ua] == uc[*ub]) && ('\0' != *a) ) andre@0: { andre@0: a++; andre@0: ua++; andre@0: ub++; andre@0: max--; andre@0: } andre@0: andre@0: if( 0 == max ) return (PRIntn)0; andre@0: andre@0: return (PRIntn)(uc[*ua] - uc[*ub]); andre@0: } andre@0: andre@0: PR_IMPLEMENT(char *) andre@0: PL_strcasestr(const char *big, const char *little) andre@0: { andre@0: PRUint32 ll; andre@0: andre@0: if( ((const char *)0 == big) || ((const char *)0 == little) ) return (char *)0; andre@0: if( ((char)0 == *big) || ((char)0 == *little) ) return (char *)0; andre@0: andre@0: ll = strlen(little); andre@0: andre@0: for( ; *big; big++ ) andre@0: /* obvious improvement available here */ andre@0: if( 0 == PL_strncasecmp(big, little, ll) ) andre@0: return (char *)big; andre@0: andre@0: return (char *)0; andre@0: } andre@0: andre@0: PR_IMPLEMENT(char *) andre@0: PL_strcaserstr(const char *big, const char *little) andre@0: { andre@0: const char *p; andre@0: PRUint32 bl, ll; andre@0: andre@0: if( ((const char *)0 == big) || ((const char *)0 == little) ) return (char *)0; andre@0: if( ((char)0 == *big) || ((char)0 == *little) ) return (char *)0; andre@0: andre@0: bl = strlen(big); andre@0: ll = strlen(little); andre@0: if( bl < ll ) return (char *)0; andre@0: p = &big[ bl - ll ]; andre@0: andre@0: for( ; p >= big; p-- ) andre@0: /* obvious improvement available here */ andre@0: if( 0 == PL_strncasecmp(p, little, ll) ) andre@0: return (char *)p; andre@0: andre@0: return (char *)0; andre@0: } andre@0: andre@0: PR_IMPLEMENT(char *) andre@0: PL_strncasestr(const char *big, const char *little, PRUint32 max) andre@0: { andre@0: PRUint32 ll; andre@0: andre@0: if( ((const char *)0 == big) || ((const char *)0 == little) ) return (char *)0; andre@0: if( ((char)0 == *big) || ((char)0 == *little) ) return (char *)0; andre@0: andre@0: ll = strlen(little); andre@0: if( ll > max ) return (char *)0; andre@0: max -= ll; andre@0: max++; andre@0: andre@0: for( ; max && *big; big++, max-- ) andre@0: /* obvious improvement available here */ andre@0: if( 0 == PL_strncasecmp(big, little, ll) ) andre@0: return (char *)big; andre@0: andre@0: return (char *)0; andre@0: } andre@0: andre@0: PR_IMPLEMENT(char *) andre@0: PL_strncaserstr(const char *big, const char *little, PRUint32 max) andre@0: { andre@0: const char *p; andre@0: PRUint32 ll; andre@0: andre@0: if( ((const char *)0 == big) || ((const char *)0 == little) ) return (char *)0; andre@0: if( ((char)0 == *big) || ((char)0 == *little) ) return (char *)0; andre@0: andre@0: ll = strlen(little); andre@0: andre@0: for( p = big; max && *p; p++, max-- ) andre@0: ; andre@0: andre@0: p -= ll; andre@0: if( p < big ) return (char *)0; andre@0: andre@0: for( ; p >= big; p-- ) andre@0: /* obvious improvement available here */ andre@0: if( 0 == PL_strncasecmp(p, little, ll) ) andre@0: return (char *)p; andre@0: andre@0: return (char *)0; andre@0: }