~ubuntu-branches/ubuntu/natty/curl/natty-proposed

« back to all changes in this revision

Viewing changes to lib/rawstr.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Schuldei
  • Date: 2009-05-24 21:12:19 UTC
  • mfrom: (1.1.12 upstream)
  • mto: (3.3.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 39.
  • Revision ID: james.westby@ubuntu.com-20090524211219-7jgcwuhl04ixuqsm
Tags: upstream-7.19.5
ImportĀ upstreamĀ versionĀ 7.19.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *                                  _   _ ____  _
 
3
 *  Project                     ___| | | |  _ \| |
 
4
 *                             / __| | | | |_) | |
 
5
 *                            | (__| |_| |  _ <| |___
 
6
 *                             \___|\___/|_| \_\_____|
 
7
 *
 
8
 * Copyright (C) 1998 - 2009, Daniel Stenberg, <daniel@haxx.se>, et al.
 
9
 *
 
10
 * This software is licensed as described in the file COPYING, which
 
11
 * you should have received as part of this distribution. The terms
 
12
 * are also available at http://curl.haxx.se/docs/copyright.html.
 
13
 *
 
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
 
15
 * copies of the Software, and permit persons to whom the Software is
 
16
 * furnished to do so, under the terms of the COPYING file.
 
17
 *
 
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 
19
 * KIND, either express or implied.
 
20
 *
 
21
 * $Id: rawstr.c,v 1.3 2009-02-07 22:53:37 bagder Exp $
 
22
 ***************************************************************************/
 
23
 
 
24
#include "setup.h"
 
25
 
 
26
#include "rawstr.h"
 
27
 
 
28
/* Portable, consistent toupper (remember EBCDIC). Do not use toupper() because
 
29
   its behavior is altered by the current locale. */
 
30
char Curl_raw_toupper(char in)
 
31
{
 
32
  switch (in) {
 
33
  case 'a':
 
34
    return 'A';
 
35
  case 'b':
 
36
    return 'B';
 
37
  case 'c':
 
38
    return 'C';
 
39
  case 'd':
 
40
    return 'D';
 
41
  case 'e':
 
42
    return 'E';
 
43
  case 'f':
 
44
    return 'F';
 
45
  case 'g':
 
46
    return 'G';
 
47
  case 'h':
 
48
    return 'H';
 
49
  case 'i':
 
50
    return 'I';
 
51
  case 'j':
 
52
    return 'J';
 
53
  case 'k':
 
54
    return 'K';
 
55
  case 'l':
 
56
    return 'L';
 
57
  case 'm':
 
58
    return 'M';
 
59
  case 'n':
 
60
    return 'N';
 
61
  case 'o':
 
62
    return 'O';
 
63
  case 'p':
 
64
    return 'P';
 
65
  case 'q':
 
66
    return 'Q';
 
67
  case 'r':
 
68
    return 'R';
 
69
  case 's':
 
70
    return 'S';
 
71
  case 't':
 
72
    return 'T';
 
73
  case 'u':
 
74
    return 'U';
 
75
  case 'v':
 
76
    return 'V';
 
77
  case 'w':
 
78
    return 'W';
 
79
  case 'x':
 
80
    return 'X';
 
81
  case 'y':
 
82
    return 'Y';
 
83
  case 'z':
 
84
    return 'Z';
 
85
  }
 
86
  return in;
 
87
}
 
88
 
 
89
/*
 
90
 * Curl_raw_equal() is for doing "raw" case insensitive strings. This is meant
 
91
 * to be locale independent and only compare strings we know are safe for
 
92
 * this.  See http://daniel.haxx.se/blog/2008/10/15/strcasecmp-in-turkish/ for
 
93
 * some further explanation to why this function is necessary.
 
94
 *
 
95
 * The function is capable of comparing a-z case insensitively even for
 
96
 * non-ascii.
 
97
 */
 
98
 
 
99
int Curl_raw_equal(const char *first, const char *second)
 
100
{
 
101
  while(*first && *second) {
 
102
    if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second))
 
103
      /* get out of the loop as soon as they don't match */
 
104
      break;
 
105
    first++;
 
106
    second++;
 
107
  }
 
108
  /* we do the comparison here (possibly again), just to make sure that if the
 
109
     loop above is skipped because one of the strings reached zero, we must not
 
110
     return this as a successful match */
 
111
  return (Curl_raw_toupper(*first) == Curl_raw_toupper(*second));
 
112
}
 
113
 
 
114
int Curl_raw_nequal(const char *first, const char *second, size_t max)
 
115
{
 
116
  while(*first && *second && max) {
 
117
    if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second)) {
 
118
      break;
 
119
    }
 
120
    max--;
 
121
    first++;
 
122
    second++;
 
123
  }
 
124
  if(0 == max)
 
125
    return 1; /* they are equal this far */
 
126
 
 
127
  return Curl_raw_toupper(*first) == Curl_raw_toupper(*second);
 
128
}
 
129
 
 
130
/* Copy an upper case version of the string from src to dest.  The
 
131
 * strings may overlap.  No more than n characters of the string are copied
 
132
 * (including any NUL) and the destination string will NOT be
 
133
 * NUL-terminated if that limit is reached.
 
134
 */
 
135
void Curl_strntoupper(char *dest, const char *src, size_t n)
 
136
{
 
137
  if (n < 1)
 
138
    return;
 
139
 
 
140
  do {
 
141
    *dest++ = Curl_raw_toupper(*src);
 
142
  } while (*src++ && --n);
 
143
}