1 /* This file is part of the Project Athena Zephyr Notification System.
2 * It is one of the source files comprising zwgc, the Zephyr WindowGram
5 * Created by: Marc Horowitz <marc@athena.mit.edu>
9 * Copyright (c) 1989 by the Massachusetts Institute of Technology.
10 * For copying and distribution information, see the file
16 #if (!defined(lint) && !defined(SABER))
17 static const char rcsid_new_string_c[] = "$Id$";
21 * string - a module providing operations on C strings. (i.e., char *'s)
25 * A string is a standard C string. I.e., a char pointer to a
26 * null-terminated sequence of characters. 0 is NOT considered a valid
27 * string! Various operations are available. See the string_spec file
30 * Note: This module assumes that malloc NEVER returns 0 for reasonable
31 * requests. It is the users responsibility to either ensure that
32 * this happens or supply a version of malloc with error
35 * Some strings are mutable.
39 #define assert(x) if (!(x)) abort()
44 #include "new_memory.h"
46 #define string_Length(s) strlen(s)
50 * string string_CreateFromData(char *data, int length):
51 * Requires: data[0], data[1], ..., data[length-1] != 0
52 * Effects: Takes the first length characters at data and
53 * creates a string containing them. The returned string
54 * is on the heap & must be freed eventually.
55 * I.e., if passed "foobar" and 3, it would return
59 string string__CreateFromData(char *data, int length)
65 result = (string)malloc(length+1);
68 (void) memcpy(result, data, length);
75 * string string_Copy(string s):
76 * Effects: Returns a copy of s on the heap. The copy must be
81 string__Copy(string s)
88 length = string_Length(s)+1;
89 result = (string)malloc(length);
92 (void) memcpy(result, s, length);
97 * string string_Concat(string a, b):
98 * Effects: Returns a string equal to a concatenated to b.
99 * The returned string is on the heap and must be
100 * freed eventually. I.e., given "abc" and "def",
101 * returns string_Copy("abcdef").
105 string__Concat(string a,
109 int a_length, b_size, result_size;
111 a_length = string_Length(a);
112 b_size = string_Length(b)+1;
113 result_size = a_length+b_size;
114 result = (string)malloc(result_size);
117 (void) memcpy(result, a, a_length);
118 (void) memcpy(result+a_length, b, b_size);
124 * string string_Concat2(string a, b):
126 * Requires: a is on the heap, b does not point into a.
127 * Effects: Equivalent to:
129 * temp = string_Concat(a,b);
132 * only faster. I.e., uses realloc instead of malloc+memcpy.
136 string__Concat2(string a,
139 int a_length = string_Length(a);
140 int b_size = string_Length(b)+1;
143 assert(memory__on_heap_p(a));
146 a = (string)realloc(a, a_length+b_size);
148 (void) memcpy(a+a_length, b, b_size);
154 * string string_Downcase(string s):
156 * Effects: Modifies s by changing every uppercase character in s
157 * to the corresponding lowercase character. Nothing else
158 * is changed. I.e., "FoObAr19." is changed to "foobar19.".
159 * S is returned as a convenience.
163 string_Downcase(string s)
167 for (ptr=s; *ptr; ptr++) {
169 *ptr = tolower(*ptr);
176 * string string_Upcase(string s):
178 * Effects: Modifies s by changing every lowercase character in s
179 * to the corresponding uppercase character. Nothing else
180 * is changed. I.e., "FoObAr19." is changed to "FOOBAR19.".
181 * S is returned as a convenience.
185 string_Upcase(string s)
189 for (ptr=s; *ptr; ptr++) {
191 *ptr = toupper(*ptr);