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
|
/*
* str.c - String helper functions that don't need string.h or ctype.h
*
* Copyright (C) 2006 Red Hat, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Author(s): David Cantrell <dcantrell@redhat.com>
*/
#include <stdio.h>
#include <stdlib.h>
#include "str.h"
/**
* Called by str2upper() or str2lower() to do the actual work.
*
* @param str String to convert.
* @param lower Lower bound for the character range (e.g., a - z).
* @param upper Upper bound for the character range (e.g., a - z).
* @param shift Shift value (32 for lowercase, -32 for uppercase).
* @return Pointer to str.
*/
char *str2case(char *str, char lower, char upper, int shift) {
char *tmp;
if (str == NULL)
return NULL;
/* man ascii(7) */
tmp = str;
while (*tmp != '\0') {
if (*tmp >= lower && *tmp <= upper)
*tmp += shift;
tmp++;
}
return str;
}
/**
* Convert given string to uppercase. Modifies the argument in the caller's
* stack. If you must ask simply "why?" for this function, it's so we don't
* need toupper() and the same for loop all over the place.
*
* LIMITATIONS: Only deals with ASCII character set.
*
* @param str String to convert to uppercase.
* @return Pointer to str.
*/
char *str2upper(char *str) {
return str2case(str, 'a', 'z', -32);
}
/**
* Convert given string to lowercase. Modifies the argument in the caller's
* stack. If you must ask simply "why?" for this function, it's so we don't
* need tolower() and the same for loop all over the place.
*
* LIMITATIONS: Only deals with ASCII character set.
*
* @param str String to convert to lowercase.
* @return Pointer to str.
*/
char *str2lower(char *str) {
return str2case(str, 'A', 'Z', 32);
}
/**
* Pretty much an exact copy of index(3) from the C library.
* @param str String to scan.
* @param ch Character to scan for.
* @return Position of ch in str, NULL if not found.
*/
char *strindex(char *str, int ch) {
if (str == NULL)
return NULL;
do {
if (*str == ch)
return str;
else
str++;
} while (*str != '\0');
return NULL;
}
/**
* Return number of occurrences of a character in a string.
* @param str String to scan.
* @param ch Character to scan for.
* @return Number of occurrences of ch in str.
*/
int strcount(char *str, int ch) {
int retval = 0;
char *tmp = str;
if (tmp == NULL)
return retval;
do {
if ((tmp = strindex(tmp, ch)) != NULL) {
tmp++;
retval++;
}
} while (tmp != NULL);
return retval;
}
/* vim:set shiftwidth=4 softtabstop=4: */
|