nsprpub/lib/libc/src/strcase.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "plstr.h"
michael@0 7 #include <string.h>
michael@0 8
michael@0 9 static const unsigned char uc[] =
michael@0 10 {
michael@0 11 '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
michael@0 12 '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
michael@0 13 '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
michael@0 14 '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
michael@0 15 ' ', '!', '"', '#', '$', '%', '&', '\'',
michael@0 16 '(', ')', '*', '+', ',', '-', '.', '/',
michael@0 17 '0', '1', '2', '3', '4', '5', '6', '7',
michael@0 18 '8', '9', ':', ';', '<', '=', '>', '?',
michael@0 19 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
michael@0 20 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
michael@0 21 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
michael@0 22 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
michael@0 23 '`', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
michael@0 24 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
michael@0 25 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
michael@0 26 'X', 'Y', 'Z', '{', '|', '}', '~', '\177',
michael@0 27 0200, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
michael@0 28 0210, 0211, 0212, 0213, 0214, 0215, 0216, 0217,
michael@0 29 0220, 0221, 0222, 0223, 0224, 0225, 0226, 0227,
michael@0 30 0230, 0231, 0232, 0233, 0234, 0235, 0236, 0237,
michael@0 31 0240, 0241, 0242, 0243, 0244, 0245, 0246, 0247,
michael@0 32 0250, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
michael@0 33 0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
michael@0 34 0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
michael@0 35 0300, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
michael@0 36 0310, 0311, 0312, 0313, 0314, 0315, 0316, 0317,
michael@0 37 0320, 0321, 0322, 0323, 0324, 0325, 0326, 0327,
michael@0 38 0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
michael@0 39 0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
michael@0 40 0350, 0351, 0352, 0353, 0354, 0355, 0356, 0357,
michael@0 41 0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
michael@0 42 0370, 0371, 0372, 0373, 0374, 0375, 0376, 0377
michael@0 43 };
michael@0 44
michael@0 45 PR_IMPLEMENT(PRIntn)
michael@0 46 PL_strcasecmp(const char *a, const char *b)
michael@0 47 {
michael@0 48 const unsigned char *ua = (const unsigned char *)a;
michael@0 49 const unsigned char *ub = (const unsigned char *)b;
michael@0 50
michael@0 51 if( ((const char *)0 == a) || (const char *)0 == b )
michael@0 52 return (PRIntn)(a-b);
michael@0 53
michael@0 54 while( (uc[*ua] == uc[*ub]) && ('\0' != *a) )
michael@0 55 {
michael@0 56 a++;
michael@0 57 ua++;
michael@0 58 ub++;
michael@0 59 }
michael@0 60
michael@0 61 return (PRIntn)(uc[*ua] - uc[*ub]);
michael@0 62 }
michael@0 63
michael@0 64 PR_IMPLEMENT(PRIntn)
michael@0 65 PL_strncasecmp(const char *a, const char *b, PRUint32 max)
michael@0 66 {
michael@0 67 const unsigned char *ua = (const unsigned char *)a;
michael@0 68 const unsigned char *ub = (const unsigned char *)b;
michael@0 69
michael@0 70 if( ((const char *)0 == a) || (const char *)0 == b )
michael@0 71 return (PRIntn)(a-b);
michael@0 72
michael@0 73 while( max && (uc[*ua] == uc[*ub]) && ('\0' != *a) )
michael@0 74 {
michael@0 75 a++;
michael@0 76 ua++;
michael@0 77 ub++;
michael@0 78 max--;
michael@0 79 }
michael@0 80
michael@0 81 if( 0 == max ) return (PRIntn)0;
michael@0 82
michael@0 83 return (PRIntn)(uc[*ua] - uc[*ub]);
michael@0 84 }
michael@0 85
michael@0 86 PR_IMPLEMENT(char *)
michael@0 87 PL_strcasestr(const char *big, const char *little)
michael@0 88 {
michael@0 89 PRUint32 ll;
michael@0 90
michael@0 91 if( ((const char *)0 == big) || ((const char *)0 == little) ) return (char *)0;
michael@0 92 if( ((char)0 == *big) || ((char)0 == *little) ) return (char *)0;
michael@0 93
michael@0 94 ll = strlen(little);
michael@0 95
michael@0 96 for( ; *big; big++ )
michael@0 97 /* obvious improvement available here */
michael@0 98 if( 0 == PL_strncasecmp(big, little, ll) )
michael@0 99 return (char *)big;
michael@0 100
michael@0 101 return (char *)0;
michael@0 102 }
michael@0 103
michael@0 104 PR_IMPLEMENT(char *)
michael@0 105 PL_strcaserstr(const char *big, const char *little)
michael@0 106 {
michael@0 107 const char *p;
michael@0 108 PRUint32 bl, ll;
michael@0 109
michael@0 110 if( ((const char *)0 == big) || ((const char *)0 == little) ) return (char *)0;
michael@0 111 if( ((char)0 == *big) || ((char)0 == *little) ) return (char *)0;
michael@0 112
michael@0 113 bl = strlen(big);
michael@0 114 ll = strlen(little);
michael@0 115 if( bl < ll ) return (char *)0;
michael@0 116 p = &big[ bl - ll ];
michael@0 117
michael@0 118 for( ; p >= big; p-- )
michael@0 119 /* obvious improvement available here */
michael@0 120 if( 0 == PL_strncasecmp(p, little, ll) )
michael@0 121 return (char *)p;
michael@0 122
michael@0 123 return (char *)0;
michael@0 124 }
michael@0 125
michael@0 126 PR_IMPLEMENT(char *)
michael@0 127 PL_strncasestr(const char *big, const char *little, PRUint32 max)
michael@0 128 {
michael@0 129 PRUint32 ll;
michael@0 130
michael@0 131 if( ((const char *)0 == big) || ((const char *)0 == little) ) return (char *)0;
michael@0 132 if( ((char)0 == *big) || ((char)0 == *little) ) return (char *)0;
michael@0 133
michael@0 134 ll = strlen(little);
michael@0 135 if( ll > max ) return (char *)0;
michael@0 136 max -= ll;
michael@0 137 max++;
michael@0 138
michael@0 139 for( ; max && *big; big++, max-- )
michael@0 140 /* obvious improvement available here */
michael@0 141 if( 0 == PL_strncasecmp(big, little, ll) )
michael@0 142 return (char *)big;
michael@0 143
michael@0 144 return (char *)0;
michael@0 145 }
michael@0 146
michael@0 147 PR_IMPLEMENT(char *)
michael@0 148 PL_strncaserstr(const char *big, const char *little, PRUint32 max)
michael@0 149 {
michael@0 150 const char *p;
michael@0 151 PRUint32 ll;
michael@0 152
michael@0 153 if( ((const char *)0 == big) || ((const char *)0 == little) ) return (char *)0;
michael@0 154 if( ((char)0 == *big) || ((char)0 == *little) ) return (char *)0;
michael@0 155
michael@0 156 ll = strlen(little);
michael@0 157
michael@0 158 for( p = big; max && *p; p++, max-- )
michael@0 159 ;
michael@0 160
michael@0 161 p -= ll;
michael@0 162 if( p < big ) return (char *)0;
michael@0 163
michael@0 164 for( ; p >= big; p-- )
michael@0 165 /* obvious improvement available here */
michael@0 166 if( 0 == PL_strncasecmp(p, little, ll) )
michael@0 167 return (char *)p;
michael@0 168
michael@0 169 return (char *)0;
michael@0 170 }

mercurial