 |
Changeset 3367
- Timestamp:
- 03/16/08 01:54:47
(6 months ago)
- Author:
- kris
- Message:
fixes #975 :: Need a case insensitive find in tango.text.Ascii
Thanks to stonecobra & nietsnie
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r2809 |
r3367 |
|
| 8 | 8 | |
|---|
| 9 | 9 | author: Kris |
|---|
| 10 | | |
|---|
| 11 | 10 | |
|---|
| 12 | 11 | Placeholder for a selection of ASCII utilities. These generally will |
|---|
| … | … | |
| 119 | 118 | /****************************************************************************** |
|---|
| 120 | 119 | |
|---|
| | 120 | Return the index position of a text pattern within src, or |
|---|
| | 121 | pattern.length upon failure. |
|---|
| | 122 | |
|---|
| | 123 | This is a case-insensitive search (with thanks to Nietsnie) |
|---|
| | 124 | |
|---|
| | 125 | ******************************************************************************/ |
|---|
| | 126 | |
|---|
| | 127 | static int isearch (in char[] src, in char[] pattern) |
|---|
| | 128 | { |
|---|
| | 129 | static char[] _caseMap = |
|---|
| | 130 | [ |
|---|
| | 131 | '\000','\001','\002','\003','\004','\005','\006','\007', |
|---|
| | 132 | '\010','\011','\012','\013','\014','\015','\016','\017', |
|---|
| | 133 | '\020','\021','\022','\023','\024','\025','\026','\027', |
|---|
| | 134 | '\030','\031','\032','\033','\034','\035','\036','\037', |
|---|
| | 135 | '\040','\041','\042','\043','\044','\045','\046','\047', |
|---|
| | 136 | '\050','\051','\052','\053','\054','\055','\056','\057', |
|---|
| | 137 | '\060','\061','\062','\063','\064','\065','\066','\067', |
|---|
| | 138 | '\070','\071','\072','\073','\074','\075','\076','\077', |
|---|
| | 139 | '\100','\141','\142','\143','\144','\145','\146','\147', |
|---|
| | 140 | '\150','\151','\152','\153','\154','\155','\156','\157', |
|---|
| | 141 | '\160','\161','\162','\163','\164','\165','\166','\167', |
|---|
| | 142 | '\170','\171','\172','\133','\134','\135','\136','\137', |
|---|
| | 143 | '\140','\141','\142','\143','\144','\145','\146','\147', |
|---|
| | 144 | '\150','\151','\152','\153','\154','\155','\156','\157', |
|---|
| | 145 | '\160','\161','\162','\163','\164','\165','\166','\167', |
|---|
| | 146 | '\170','\171','\172','\173','\174','\175','\176','\177', |
|---|
| | 147 | '\200','\201','\202','\203','\204','\205','\206','\207', |
|---|
| | 148 | '\210','\211','\212','\213','\214','\215','\216','\217', |
|---|
| | 149 | '\220','\221','\222','\223','\224','\225','\226','\227', |
|---|
| | 150 | '\230','\231','\232','\233','\234','\235','\236','\237', |
|---|
| | 151 | '\240','\241','\242','\243','\244','\245','\246','\247', |
|---|
| | 152 | '\250','\251','\252','\253','\254','\255','\256','\257', |
|---|
| | 153 | '\260','\261','\262','\263','\264','\265','\266','\267', |
|---|
| | 154 | '\270','\271','\272','\273','\274','\275','\276','\277', |
|---|
| | 155 | '\300','\341','\342','\343','\344','\345','\346','\347', |
|---|
| | 156 | '\350','\351','\352','\353','\354','\355','\356','\357', |
|---|
| | 157 | '\360','\361','\362','\363','\364','\365','\366','\367', |
|---|
| | 158 | '\370','\371','\372','\333','\334','\335','\336','\337', |
|---|
| | 159 | '\340','\341','\342','\343','\344','\345','\346','\347', |
|---|
| | 160 | '\350','\351','\352','\353','\354','\355','\356','\357', |
|---|
| | 161 | '\360','\361','\362','\363','\364','\365','\366','\367', |
|---|
| | 162 | '\370','\371','\372','\373','\374','\375','\376','\377', |
|---|
| | 163 | ]; |
|---|
| | 164 | |
|---|
| | 165 | |
|---|
| | 166 | assert(src.ptr); |
|---|
| | 167 | assert(pattern.ptr); |
|---|
| | 168 | |
|---|
| | 169 | for (uint i2, i1=0; i1 < src.length; ++i1) |
|---|
| | 170 | { |
|---|
| | 171 | for (i2=0; i2 < pattern.length; ++i2) |
|---|
| | 172 | if (_caseMap[src[i1 + i2]] != _caseMap[pattern[i2]]) |
|---|
| | 173 | break; |
|---|
| | 174 | |
|---|
| | 175 | if (i2 is pattern.length) |
|---|
| | 176 | return i1; |
|---|
| | 177 | } |
|---|
| | 178 | return src.length; |
|---|
| | 179 | } |
|---|
| | 180 | |
|---|
| | 181 | |
|---|
| | 182 | |
|---|
| | 183 | /****************************************************************************** |
|---|
| | 184 | |
|---|
| 121 | 185 | ******************************************************************************/ |
|---|
| 122 | 186 | |
|---|
| 123 | 187 | debug (UnitTest) |
|---|
| 124 | 188 | { |
|---|
| 125 | | // void main(){} |
|---|
| | 189 | //void main(){} |
|---|
| 126 | 190 | |
|---|
| 127 | 191 | unittest |
|---|
| … | … | |
| 138 | 202 | assert (icompare ("abc", "abcd") < 0); |
|---|
| 139 | 203 | assert (icompare ("ACC", "abc") > 0); |
|---|
| | 204 | |
|---|
| | 205 | assert (isearch ("ACC", "abc") is 3); |
|---|
| | 206 | assert (isearch ("ACC", "acc") is 0); |
|---|
| | 207 | assert (isearch ("aACC", "acc") is 1); |
|---|
| 140 | 208 | } |
|---|
| 141 | 209 | } |
|---|
Download in other formats:
|
 |