strstr.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "plstr.h"
  6. #include <string.h>
  7. PR_IMPLEMENT(char *)
  8. PL_strstr(const char *big, const char *little)
  9. {
  10. if( ((const char *)0 == big) || ((const char *)0 == little) ) {
  11. return (char *)0;
  12. }
  13. if( ((char)0 == *big) || ((char)0 == *little) ) {
  14. return (char *)0;
  15. }
  16. return strstr(big, little);
  17. }
  18. PR_IMPLEMENT(char *)
  19. PL_strrstr(const char *big, const char *little)
  20. {
  21. const char *p;
  22. size_t ll;
  23. size_t bl;
  24. if( ((const char *)0 == big) || ((const char *)0 == little) ) {
  25. return (char *)0;
  26. }
  27. if( ((char)0 == *big) || ((char)0 == *little) ) {
  28. return (char *)0;
  29. }
  30. ll = strlen(little);
  31. bl = strlen(big);
  32. if( bl < ll ) {
  33. return (char *)0;
  34. }
  35. p = &big[ bl - ll ];
  36. for( ; p >= big; p-- )
  37. if( *little == *p )
  38. if( 0 == strncmp(p, little, ll) ) {
  39. return (char *)p;
  40. }
  41. return (char *)0;
  42. }
  43. PR_IMPLEMENT(char *)
  44. PL_strnstr(const char *big, const char *little, PRUint32 max)
  45. {
  46. size_t ll;
  47. if( ((const char *)0 == big) || ((const char *)0 == little) ) {
  48. return (char *)0;
  49. }
  50. if( ((char)0 == *big) || ((char)0 == *little) ) {
  51. return (char *)0;
  52. }
  53. ll = strlen(little);
  54. if( ll > (size_t)max ) {
  55. return (char *)0;
  56. }
  57. max -= (PRUint32)ll;
  58. max++;
  59. for( ; max && *big; big++, max-- )
  60. if( *little == *big )
  61. if( 0 == strncmp(big, little, ll) ) {
  62. return (char *)big;
  63. }
  64. return (char *)0;
  65. }
  66. PR_IMPLEMENT(char *)
  67. PL_strnrstr(const char *big, const char *little, PRUint32 max)
  68. {
  69. const char *p;
  70. size_t ll;
  71. if( ((const char *)0 == big) || ((const char *)0 == little) ) {
  72. return (char *)0;
  73. }
  74. if( ((char)0 == *big) || ((char)0 == *little) ) {
  75. return (char *)0;
  76. }
  77. ll = strlen(little);
  78. for( p = big; max && *p; p++, max-- )
  79. ;
  80. p -= ll;
  81. if( p < big ) {
  82. return (char *)0;
  83. }
  84. for( ; p >= big; p-- )
  85. if( *little == *p )
  86. if( 0 == strncmp(p, little, ll) ) {
  87. return (char *)p;
  88. }
  89. return (char *)0;
  90. }