Fri, 16 Jan 2015 04:50:19 +0100
Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
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 | PR_IMPLEMENT(char *) |
michael@0 | 10 | PL_strstr(const char *big, const char *little) |
michael@0 | 11 | { |
michael@0 | 12 | if( ((const char *)0 == big) || ((const char *)0 == little) ) return (char *)0; |
michael@0 | 13 | if( ((char)0 == *big) || ((char)0 == *little) ) return (char *)0; |
michael@0 | 14 | |
michael@0 | 15 | return strstr(big, little); |
michael@0 | 16 | } |
michael@0 | 17 | |
michael@0 | 18 | PR_IMPLEMENT(char *) |
michael@0 | 19 | PL_strrstr(const char *big, const char *little) |
michael@0 | 20 | { |
michael@0 | 21 | const char *p; |
michael@0 | 22 | size_t ll; |
michael@0 | 23 | size_t bl; |
michael@0 | 24 | |
michael@0 | 25 | if( ((const char *)0 == big) || ((const char *)0 == little) ) return (char *)0; |
michael@0 | 26 | if( ((char)0 == *big) || ((char)0 == *little) ) return (char *)0; |
michael@0 | 27 | |
michael@0 | 28 | ll = strlen(little); |
michael@0 | 29 | bl = strlen(big); |
michael@0 | 30 | if( bl < ll ) return (char *)0; |
michael@0 | 31 | p = &big[ bl - ll ]; |
michael@0 | 32 | |
michael@0 | 33 | for( ; p >= big; p-- ) |
michael@0 | 34 | if( *little == *p ) |
michael@0 | 35 | if( 0 == strncmp(p, little, ll) ) |
michael@0 | 36 | return (char *)p; |
michael@0 | 37 | |
michael@0 | 38 | return (char *)0; |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | PR_IMPLEMENT(char *) |
michael@0 | 42 | PL_strnstr(const char *big, const char *little, PRUint32 max) |
michael@0 | 43 | { |
michael@0 | 44 | size_t ll; |
michael@0 | 45 | |
michael@0 | 46 | if( ((const char *)0 == big) || ((const char *)0 == little) ) return (char *)0; |
michael@0 | 47 | if( ((char)0 == *big) || ((char)0 == *little) ) return (char *)0; |
michael@0 | 48 | |
michael@0 | 49 | ll = strlen(little); |
michael@0 | 50 | if( ll > (size_t)max ) return (char *)0; |
michael@0 | 51 | max -= (PRUint32)ll; |
michael@0 | 52 | max++; |
michael@0 | 53 | |
michael@0 | 54 | for( ; max && *big; big++, max-- ) |
michael@0 | 55 | if( *little == *big ) |
michael@0 | 56 | if( 0 == strncmp(big, little, ll) ) |
michael@0 | 57 | return (char *)big; |
michael@0 | 58 | |
michael@0 | 59 | return (char *)0; |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | PR_IMPLEMENT(char *) |
michael@0 | 63 | PL_strnrstr(const char *big, const char *little, PRUint32 max) |
michael@0 | 64 | { |
michael@0 | 65 | const char *p; |
michael@0 | 66 | size_t ll; |
michael@0 | 67 | |
michael@0 | 68 | if( ((const char *)0 == big) || ((const char *)0 == little) ) return (char *)0; |
michael@0 | 69 | if( ((char)0 == *big) || ((char)0 == *little) ) return (char *)0; |
michael@0 | 70 | |
michael@0 | 71 | ll = strlen(little); |
michael@0 | 72 | |
michael@0 | 73 | for( p = big; max && *p; p++, max-- ) |
michael@0 | 74 | ; |
michael@0 | 75 | |
michael@0 | 76 | p -= ll; |
michael@0 | 77 | if( p < big ) return (char *)0; |
michael@0 | 78 | |
michael@0 | 79 | for( ; p >= big; p-- ) |
michael@0 | 80 | if( *little == *p ) |
michael@0 | 81 | if( 0 == strncmp(p, little, ll) ) |
michael@0 | 82 | return (char *)p; |
michael@0 | 83 | |
michael@0 | 84 | return (char *)0; |
michael@0 | 85 | } |