The R Project SVN R

Rev

Rev 4394 | Rev 42521 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4394 Rev 41793
Line 26... Line 26...
26
 
26
 
27
/*
27
/*
28
 *  Create and return a newly allocated copy of a string.
28
 *  Create and return a newly allocated copy of a string.
29
 *  Null strings are converted into empty strings first.
29
 *  Null strings are converted into empty strings first.
30
 */
30
 */
31
char *new_string(char *src)
31
char *new_string(const char *src)
32
{
32
{
33
	char *str;
33
	char *str;
34
	if (! src)
34
	if (! src)
35
		src = "";
35
		src = "";
36
	str = array (string_length(src), char);
36
	str = array (string_length(src), char);
37
	if (str)
37
	if (str)
38
		copy_string(str, src);
38
		copy_string(str, src);
39
	return str;
39
	return str;
40
}
40
}
41
 
41
 
42
void del_string(char *str)
42
void del_string(const char *str)
43
{
43
{
44
	discard(str);
44
	discard(str);
45
}
45
}
46
 
46
 
47
/*
47
/*
48
 *  String length function.
48
 *  String length function.
49
 *  If the string is null, the length is defined to be zero.
49
 *  If the string is null, the length is defined to be zero.
50
 */
50
 */
51
long string_length(char *s)
51
long string_length(const char *s)
52
{
52
{
53
	long len = 0;
53
	long len = 0;
54
	if (s)
54
	if (s)
55
		while (s[len])
55
		while (s[len])
56
			len++;
56
			len++;
Line 59... Line 59...
59
 
59
 
60
/*
60
/*
61
 *  Copy a string.
61
 *  Copy a string.
62
 *  Same as strcpy except it avoids doing anything to null strings.
62
 *  Same as strcpy except it avoids doing anything to null strings.
63
 */
63
 */
64
void copy_string(char *dest, char *src)
64
void copy_string(char *dest, const char *src)
65
{
65
{
66
	int len;
66
	int len;
67
	if ((dest) && (src)) {
67
	if ((dest) && (src)) {
68
		for (len=0; src[len]; len++)
68
		for (len=0; src[len]; len++)
69
			dest[len] = src[len];
69
			dest[len] = src[len];
Line 73... Line 73...
73
 
73
 
74
/*
74
/*
75
 *  String comparison function.
75
 *  String comparison function.
76
 *  Null == null, null == "", "" == null, otherwise same as strcmp.
76
 *  Null == null, null == "", "" == null, otherwise same as strcmp.
77
 */
77
 */
78
int compare_strings(char *s1, char *s2)
78
int compare_strings(const char *s1, const char *s2)
79
{
79
{
80
	int len, diff;
80
	int len, diff;
81
 
81
 
82
	if (s1 == s2)
82
	if (s1 == s2)
83
		return 0;
83
		return 0;
Line 105... Line 105...
105
 *  Append one string to another, and return the result.
105
 *  Append one string to another, and return the result.
106
 *  This function correctly handles the situation where one or both
106
 *  This function correctly handles the situation where one or both
107
 *  of the parameters are temporary strings returned by an earlier
107
 *  of the parameters are temporary strings returned by an earlier
108
 *  call to this function.
108
 *  call to this function.
109
 */
109
 */
110
char *add_strings(char *s1, char *s2)
110
const char *add_strings(const char *s1, const char *s2)
111
{
111
{
112
	static char *buffer = NULL;
112
	static char *buffer = NULL;
113
	char *prev;
113
	char *prev;
114
	int len1, len2;
114
	int len1, len2;
115
 
115
 
Line 137... Line 137...
137
}
137
}
138
 
138
 
139
/*
139
/*
140
 *  Convert a char to a string, return the result in a static buffer.
140
 *  Convert a char to a string, return the result in a static buffer.
141
 */
141
 */
142
char *char_to_string(char ch)
142
char *char_to_string(const char ch)
143
{
143
{
144
	static char str[2];
144
	static char str[2];
145
	str[0] = ch;
145
	str[0] = ch;
146
	str[1] = '\0';
146
	str[1] = '\0';
147
	return str;
147
	return str;
Line 174... Line 174...
174
 
174
 
175
/*
175
/*
176
 *  Case-insensitive string comparison function.
176
 *  Case-insensitive string comparison function.
177
 */
177
 */
178
PROTECTED
178
PROTECTED
179
int string_diff(char *s, char *t)
179
int string_diff(const char *s, const char *t)
180
{
180
{
181
	int ch1, ch2;
181
	int ch1, ch2;
182
	int diff = 0;
182
	int diff = 0;
183
 
183
 
184
	while ((diff == 0) && ((*s != '\0') || (*t != '\0')))
184
	while ((diff == 0) && ((*s != '\0') || (*t != '\0')))
Line 198... Line 198...
198
/*
198
/*
199
 *  Produce a new string which has CR-LF instead of just '\n'
199
 *  Produce a new string which has CR-LF instead of just '\n'
200
 *  to mark end of line. The returned string must later be freed.
200
 *  to mark end of line. The returned string must later be freed.
201
 */
201
 */
202
PROTECTED
202
PROTECTED
203
char *to_dos_string(char *text)
203
char *to_dos_string(const char *text)
204
{
204
{
205
	char *s;
205
	const char *s;
206
	char *newstr;
206
	char *newstr, *ss;
207
	char prev;
207
	char prev;
208
	long length = 0;
208
	long length = 0;
209
 
209
 
210
	if (!text)
210
	if (!text)
211
		return text;
211
		return (char *) NULL;
212
	prev = '\0';
212
	prev = '\0';
213
	for (s = text; *s != '\0'; s++) {
213
	for (s = text; *s != '\0'; s++) {
214
		length++;
214
		length++;
215
		if ((*s == '\n') && (prev != '\r'))
215
		if ((*s == '\n') && (prev != '\r'))
216
			length++;
216
			length++;
Line 218... Line 218...
218
	}
218
	}
219
 
219
 
220
	newstr = array (length, char);
220
	newstr = array (length, char);
221
 
221
 
222
	prev = '\0';
222
	prev = '\0';
223
	for (s = newstr; *text != '\0'; s++, text++) {
223
	for (ss = newstr; *text != '\0'; ss++, text++) {
224
		if ((*text == '\n') && (prev != '\r')) {
224
		if ((*text == '\n') && (prev != '\r')) {
225
			*s = '\r';
225
			*ss = '\r';
226
			s++;
226
			ss++;
227
		}
227
		}
228
		*s = *text;
228
		*ss = *text;
229
		prev = *s;
229
		prev = *ss;
230
	}
230
	}
231
	*s = '\0';
231
	*ss = '\0';
232
 
232
 
233
	return newstr;
233
	return newstr;
234
}
234
}
235
 
235
 
236
/*
236
/*
237
 *  Strip carriage returns from a string. The resulting
237
 *  Strip carriage returns from a string. The resulting
238
 *  string must later be freed.
238
 *  string must later be freed.
239
 */
239
 */
240
PROTECTED
240
PROTECTED
241
char *to_c_string(char *text)
241
char *to_c_string(const char *text)
242
{
242
{
243
	char *s;
243
	const char *s;
244
	char *newstr;
244
	char *newstr, *ss;
245
	long length = 0;
245
	long length = 0;
246
 
246
 
247
	if (!text)
247
	if (!text)
248
		return text;
248
		return (char *) NULL;
249
	for (s = text; *s != '\0'; s++) {
249
	for (s = text; *s != '\0'; s++) {
250
		length++;
250
		length++;
251
		if ((*s == '\r') && (*(s+1) == '\n'))
251
		if ((*s == '\r') && (*(s+1) == '\n'))
252
			length--;
252
			length--;
253
	}
253
	}
254
 
254
 
255
	newstr = array (length, char);
255
	newstr = array (length, char);
256
 
256
 
257
	for (s = newstr; *text != '\0'; s++, text++) {
257
	for (ss = newstr; *text != '\0'; ss++, text++) {
258
		if ((*text == '\r') && (*(text+1) == '\n'))
258
		if ((*text == '\r') && (*(text+1) == '\n'))
259
			text++;
259
			text++;
260
		*s = *text;
260
		*ss = *text;
261
	}
261
	}
262
	*s = '\0';
262
	*ss = '\0';
263
 
263
 
264
	return newstr;
264
	return newstr;
265
}
265
}