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>
7 * $Id: new_string.c,v 1.2 1999/01/22 23:20:26 ghudson Exp $
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: new_string.c,v 1.2 1999/01/22 23:20:26 ghudson Exp $";
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(data, length)
67 result = (string)malloc(length+1);
70 (void) memcpy(result, data, length);
77 * string string_Copy(string s):
78 * Effects: Returns a copy of s on the heap. The copy must be
82 string string__Copy(s)
90 length = string_Length(s)+1;
91 result = (string)malloc(length);
94 (void) memcpy(result, s, length);
99 * string string_Concat(string a, b):
100 * Effects: Returns a string equal to a concatenated to b.
101 * The returned string is on the heap and must be
102 * freed eventually. I.e., given "abc" and "def",
103 * returns string_Copy("abcdef").
106 string string__Concat(a, b)
110 int a_length, b_size, result_size;
112 a_length = string_Length(a);
113 b_size = string_Length(b)+1;
114 result_size = a_length+b_size;
115 result = (string)malloc(result_size);
118 (void) memcpy(result, a, a_length);
119 (void) memcpy(result+a_length, b, b_size);
125 * string string_Concat2(string a, b):
127 * Requires: a is on the heap, b does not point into a.
128 * Effects: Equivalent to:
130 * temp = string_Concat(a,b);
133 * only faster. I.e., uses realloc instead of malloc+memcpy.
136 string string__Concat2(a, b)
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.
162 string string_Downcase(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.
184 string string_Upcase(s)
189 for (ptr=s; *ptr; ptr++) {
191 *ptr = toupper(*ptr);