]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/zwgc/new_string.c
r4264@bucket (orig r254): kcr | 2008-01-20 22:11:44 -0500
[1ts-debian.git] / zephyr / zwgc / new_string.c
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
3  * client.
4  *
5  *      Created by:     Marc Horowitz <marc@athena.mit.edu>
6  *
7  *      $Id$
8  *
9  *      Copyright (c) 1989 by the Massachusetts Institute of Technology.
10  *      For copying and distribution information, see the file
11  *      "mit-copyright.h".
12  */
13
14 #include <sysdep.h>
15
16 #if (!defined(lint) && !defined(SABER))
17 static const char rcsid_new_string_c[] = "$Id$";
18 #endif
19
20 /*
21  * string - a module providing operations on C strings.  (i.e., char *'s)
22  *
23  * Overview:
24  *
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
28  *    for details.
29  *
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
33  *          handling.
34  *
35  *    Some strings are mutable.
36  */
37
38 #ifdef DEBUG
39 #define  assert(x)          if (!(x)) abort()
40 #else
41 #define  assert(x)          
42 #endif
43
44 #include "new_memory.h"
45
46 #define string_Length(s) strlen(s)
47 typedef char *string;
48
49 /*
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
56  *                 string_Copy("foo").
57  */
58
59 string string__CreateFromData(char *data, int length)
60 {
61     string result;
62
63     assert(length>=0);
64
65     result = (string)malloc(length+1);
66     assert(result);
67
68     (void) memcpy(result, data, length);
69     result[length] = 0;
70
71     return(result);
72 }
73
74 /*
75  *    string string_Copy(string s):
76  *        Effects: Returns a copy of s on the heap.  The copy must be
77  *                 freed eventually.
78  */
79
80 string
81 string__Copy(string s)
82 {
83     int length;
84     string result;
85
86     assert(s);
87
88     length = string_Length(s)+1;
89     result = (string)malloc(length);
90     assert(result);
91
92     (void) memcpy(result, s, length);
93     return(result);
94 }
95
96 /*
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").
102  */
103
104 string
105 string__Concat(string a,
106                string b)
107 {
108     string result;
109     int a_length, b_size, result_size;
110
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);
115     assert(result);
116
117     (void) memcpy(result, a, a_length);
118     (void) memcpy(result+a_length, b, b_size);
119
120     return(result);
121 }
122
123 /*
124  *    string string_Concat2(string a, b):
125  *        Modifies: a
126  *        Requires: a is on the heap, b does not point into a.
127  *        Effects: Equivalent to:
128  *                     string temp;
129  *                     temp = string_Concat(a,b);
130  *                     free(a);
131  *                     return(temp);
132  *                 only faster.  I.e., uses realloc instead of malloc+memcpy.
133  */
134
135 string
136 string__Concat2(string a,
137                 string b)
138 {
139     int a_length = string_Length(a);
140     int b_size = string_Length(b)+1;
141
142 #ifdef DEBUG_MEMORY
143     assert(memory__on_heap_p(a));
144 #endif
145
146     a = (string)realloc(a, a_length+b_size);
147     assert(a);
148     (void) memcpy(a+a_length, b, b_size);
149
150     return(a);
151 }
152
153 /*
154  *    string string_Downcase(string s):
155  *        Modifies: 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.
160  */
161
162 string
163 string_Downcase(string s)
164 {
165     char *ptr;
166
167     for (ptr=s; *ptr; ptr++) {
168         if (isupper(*ptr))
169           *ptr = tolower(*ptr);
170     }
171
172     return(s);
173 }
174
175 /*
176  *    string string_Upcase(string s):
177  *        Modifies: 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.
182  */
183
184 string
185 string_Upcase(string s)
186 {
187     char *ptr;
188
189     for (ptr=s; *ptr; ptr++) {
190         if (islower(*ptr))
191           *ptr = toupper(*ptr);
192     }
193
194     return(s);
195 }