CUBRID Engine  latest
misc_string.c
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 /*
20  * misc_string.c : case insensitive string comparison routines for 8-bit
21  * character sets
22  */
23 
24 #ident "$Id$"
25 
26 #include <assert.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <ctype.h>
31 
32 #include "misc_string.h"
33 
34 /*
35  * ustr_casestr() - find a substring, case ignored
36  * return: char*
37  * s1(in)
38  * s2(in)
39  */
40 char *
41 ustr_casestr (const char *s1, const char *s2)
42 {
43  size_t len1, len2;
44  const char *p1, *p2;
45  int c;
46 
47  if (!s1 || !s2)
48  return NULL;
49  if (s1 == s2)
50  return (char *) s1;
51  if (!*s2)
52  return (char *) s1;
53  if (!*s1)
54  return NULL;
55 
56  len1 = strlen (s1);
57  len2 = strlen (s2);
58  c = tolower (*s2);
59  while (len1 >= len2)
60  {
61  while (tolower (*s1) != c)
62  {
63  if (--len1 < len2)
64  return NULL;
65  s1++;
66  }
67 
68  p1 = s1;
69  p2 = s2;
70  while (tolower (*p1) == tolower (*p2))
71  {
72  p1++;
73  p2++;
74  if (!*p2)
75  return (char *) s1;
76  }
77 
78  if (p1 == s1)
79  {
80  len1--;
81  s1--;
82  }
83  else
84  {
85  len1 -= (p1 - s1) + 1;
86  s1 += (p1 - s1) + 1;
87  }
88  }
89  return NULL;
90 }
91 
92 /*
93  * ustr_upper() - replace all lower case characters with upper case characters
94  * return: char *
95  * s(in/out)
96  */
97 char *
98 ustr_upper (char *s)
99 {
100  char *t;
101 
102  if (!s)
103  return NULL;
104 
105  for (t = s; *t; t++)
106  *t = toupper (*t);
107  return s;
108 }
109 
110 /*
111  * ustr_lower() - replace all upper case characters with lower case characters
112  * return: char *
113  * s(in/out)
114  */
115 char *
116 ustr_lower (char *s)
117 {
118  char *t;
119 
120  if (!s)
121  return NULL;
122 
123  for (t = s; *t; t++)
124  *t = tolower (*t);
125  return s;
126 }
char * ustr_upper(char *s)
Definition: misc_string.c:98
char * ustr_lower(char *s)
Definition: misc_string.c:116
#define NULL
Definition: freelistheap.h:34
#define strlen(s1)
Definition: intl_support.c:43
char * ustr_casestr(const char *s1, const char *s2)
Definition: misc_string.c:41