File: ustr-cmp-code.h

package info (click to toggle)
ustr 1.0.4-6.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,676 kB
  • sloc: ansic: 19,041; makefile: 935; perl: 779; sh: 686; xml: 97
file content (207 lines) | stat: -rw-r--r-- 5,042 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* Copyright (c) 2007 James Antill -- See LICENSE file for terms. */

#ifndef USTR_CMP_H
#error " You should have already included ustr-cmp.h, or just include ustr.h."
#endif

USTR_CONF_I_PROTO
int ustr_cmp_buf(const struct Ustr *s1, const void *buf, size_t len2)
{
  size_t len1 = 0;
  size_t lenm = 0;
  int    ret = 0;
  int    def = 0;
  
  USTR_ASSERT(ustr_assert_valid(s1) && buf);

  len1 = ustr_len(s1);
  if (len1 == len2)
    return (memcmp(ustr_cstr(s1), buf, len1));

  if (len1 > len2)
  {
    lenm = len2;
    def  =  1;
  }
  else
  {
    lenm = len1;
    def  = -1;
  }

  if (lenm && (ret = memcmp(ustr_cstr(s1), buf, lenm)))
    return (ret);
  
  return (def);
}

USTR_CONF_I_PROTO
int ustr_cmp_subustr(const struct Ustr *s1,
                     const struct Ustr *s2, size_t pos, size_t len)
{
  USTR_ASSERT(ustr_assert_valid(s1) && ustr_assert_valid(s2));

  if (!ustr_assert_valid_subustr(s2, pos, len))
    return (ustr_cmp_buf(s1, "", 0));

  return (ustr_cmp_buf(s1, ustr_cstr(s2) + --pos, len));
}

USTR_CONF_i_PROTO
int ustr__memcasecmp(const void *passed_s1, const void *passed_s2, size_t len)
{
  const unsigned char *s1 = passed_s1;
  const unsigned char *s2 = passed_s2;

  while (len)
  {
    unsigned char c1 = *s1;
    unsigned char c2 = *s2;

    if ((c1 >= 0x61) && (c1 <= 0x7a))
      c1 ^= 0x20;
    if ((c2 >= 0x61) && (c2 <= 0x7a))
      c2 ^= 0x20;
    
    if (c1 != c2)
      return (c1 - c2);

    ++s1;
    ++s2;
    --len;
  }

  return (0);
}

USTR_CONF_I_PROTO
int ustr_cmp_case_buf(const struct Ustr *s1, const void *buf, size_t len2)
{
  size_t len1 = 0;
  size_t lenm = 0;
  int    ret = 0;
  int    def = 0;
  
  USTR_ASSERT(ustr_assert_valid(s1) && buf);

  len1 = ustr_len(s1);
  if (len1 == len2)
    return (ustr__memcasecmp(ustr_cstr(s1), buf, len1));

  if (len1 > len2)
  {
    lenm = len2;
    def  =  1;
  }
  else
  {
    lenm = len1;
    def  = -1;
  }

  if (lenm && (ret = ustr__memcasecmp(ustr_cstr(s1), buf, lenm)))
    return (ret);
  
  return (def);
}

USTR_CONF_I_PROTO
int ustr_cmp_case_subustr(const struct Ustr *s1,
                          const struct Ustr *s2, size_t pos, size_t len)
{
  USTR_ASSERT(ustr_assert_valid(s1) && ustr_assert_valid(s2));

  if (!ustr_assert_valid_subustr(s2, pos, len))
    return (ustr_cmp_case_buf(s1, "", 0));

  return (ustr_cmp_case_buf(s1, ustr_cstr(s2) + --pos, len));
}

USTR_CONF_I_PROTO int ustr_cmp_case_prefix_buf_eq(const struct Ustr *s1,
                                                  const void *buf, size_t len2)
{
  size_t len1 = 0;
  
  USTR_ASSERT(ustr_assert_valid(s1) && buf);
  
  len1 = ustr_len(s1);
  if (len1 < len2)
    return (USTR_FALSE);
  
  return (!ustr__memcasecmp(ustr_cstr(s1), buf, len2));
}

USTR_CONF_I_PROTO int ustr_cmp_case_suffix_buf_eq(const struct Ustr *s1,
                                                  const void *buf, size_t len2)
{
  size_t len1 = 0;
  
  USTR_ASSERT(ustr_assert_valid(s1) && buf);
  
  len1 = ustr_len(s1);
  if (len1 < len2)
    return (USTR_FALSE);
  
  return (!ustr__memcasecmp(ustr_cstr(s1) + (len1 - len2), buf, len2));
}

USTR_CONF_I_PROTO
int ustr_cmp_fast_subustr(const struct Ustr *s1,
                          const struct Ustr *s2, size_t pos, size_t len)
{
  USTR_ASSERT(ustr_assert_valid(s1) && ustr_assert_valid(s2));

  if (!ustr_assert_valid_subustr(s2, pos, len))
    return (ustr_cmp_fast_buf(s1, "", 0));

  return (ustr_cmp_fast_buf(s1, ustr_cstr(s2) + --pos, len));
}

USTR_CONF_I_PROTO
int ustr_cmp_prefix_subustr_eq(const struct Ustr *s1,
                               const struct Ustr *s2, size_t pos, size_t len)
{
  USTR_ASSERT(ustr_assert_valid(s1) && ustr_assert_valid(s2));

  if (!ustr_assert_valid_subustr(s2, pos, len))
    return (ustr_cmp_prefix_buf_eq(s1, "", 0));

  return (ustr_cmp_prefix_buf_eq(s1, ustr_cstr(s2) + --pos, len));
}

USTR_CONF_I_PROTO
int ustr_cmp_suffix_subustr_eq(const struct Ustr *s1,
                               const struct Ustr *s2, size_t pos, size_t len)
{
  USTR_ASSERT(ustr_assert_valid(s1) && ustr_assert_valid(s2));

  if (!ustr_assert_valid_subustr(s2, pos, len))
    return (ustr_cmp_suffix_buf_eq(s1, "", 0));

  return (ustr_cmp_suffix_buf_eq(s1, ustr_cstr(s2) + --pos, len));
}

USTR_CONF_I_PROTO
int ustr_cmp_case_prefix_subustr_eq(const struct Ustr *s1,
                                    const struct Ustr *s2,size_t pos,size_t len)
{
  USTR_ASSERT(ustr_assert_valid(s1) && ustr_assert_valid(s2));

  if (!ustr_assert_valid_subustr(s2, pos, len))
    return (ustr_cmp_case_prefix_buf_eq(s1, "", 0));

  return (ustr_cmp_case_prefix_buf_eq(s1, ustr_cstr(s2) + --pos, len));
}

USTR_CONF_I_PROTO
int ustr_cmp_case_suffix_subustr_eq(const struct Ustr *s1,
                                    const struct Ustr *s2,size_t pos,size_t len)
{
  USTR_ASSERT(ustr_assert_valid(s1) && ustr_assert_valid(s2));

  if (!ustr_assert_valid_subustr(s2, pos, len))
    return (ustr_cmp_case_suffix_buf_eq(s1, "", 0));

  return (ustr_cmp_case_suffix_buf_eq(s1, ustr_cstr(s2) + --pos, len));
}