xref: /illumos-gate/usr/src/cmd/tr/cmap.c (revision 803376f0)
1*163bd69bSGarrett D'Amore /*
2*163bd69bSGarrett D'Amore  * Copyright (c) 2004 Tim J. Robbins.
3*163bd69bSGarrett D'Amore  * All rights reserved.
4*163bd69bSGarrett D'Amore  *
5*163bd69bSGarrett D'Amore  * Redistribution and use in source and binary forms, with or without
6*163bd69bSGarrett D'Amore  * modification, are permitted provided that the following conditions
7*163bd69bSGarrett D'Amore  * are met:
8*163bd69bSGarrett D'Amore  * 1. Redistributions of source code must retain the above copyright
9*163bd69bSGarrett D'Amore  *    notice, this list of conditions and the following disclaimer.
10*163bd69bSGarrett D'Amore  * 2. Redistributions in binary form must reproduce the above copyright
11*163bd69bSGarrett D'Amore  *    notice, this list of conditions and the following disclaimer in the
12*163bd69bSGarrett D'Amore  *    documentation and/or other materials provided with the distribution.
13*163bd69bSGarrett D'Amore  *
14*163bd69bSGarrett D'Amore  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*163bd69bSGarrett D'Amore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*163bd69bSGarrett D'Amore  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*163bd69bSGarrett D'Amore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*163bd69bSGarrett D'Amore  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*163bd69bSGarrett D'Amore  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*163bd69bSGarrett D'Amore  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*163bd69bSGarrett D'Amore  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*163bd69bSGarrett D'Amore  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*163bd69bSGarrett D'Amore  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*163bd69bSGarrett D'Amore  * SUCH DAMAGE.
25*163bd69bSGarrett D'Amore  */
26*163bd69bSGarrett D'Amore /*
27*163bd69bSGarrett D'Amore  * "Character map" ADT. Stores mappings between pairs of characters in a
28*163bd69bSGarrett D'Amore  * splay tree, with a lookup table cache to simplify looking up the first
29*163bd69bSGarrett D'Amore  * bunch of characters (which are presumably more common than others).
30*163bd69bSGarrett D'Amore  */
31*163bd69bSGarrett D'Amore 
32*163bd69bSGarrett D'Amore #include <assert.h>
33*163bd69bSGarrett D'Amore #include <limits.h>
34*163bd69bSGarrett D'Amore #include <stdbool.h>
35*163bd69bSGarrett D'Amore #include <stdlib.h>
36*163bd69bSGarrett D'Amore #include <wchar.h>
37*163bd69bSGarrett D'Amore #include "cmap.h"
38*163bd69bSGarrett D'Amore 
39*163bd69bSGarrett D'Amore static struct cmapnode *cmap_splay(struct cmapnode *, wint_t);
40*163bd69bSGarrett D'Amore 
41*163bd69bSGarrett D'Amore /*
42*163bd69bSGarrett D'Amore  * cmap_alloc --
43*163bd69bSGarrett D'Amore  *	Allocate a character map.
44*163bd69bSGarrett D'Amore  */
45*163bd69bSGarrett D'Amore struct cmap *
cmap_alloc(void)46*163bd69bSGarrett D'Amore cmap_alloc(void)
47*163bd69bSGarrett D'Amore {
48*163bd69bSGarrett D'Amore 	struct cmap *cm;
49*163bd69bSGarrett D'Amore 
50*163bd69bSGarrett D'Amore 	cm = malloc(sizeof (*cm));
51*163bd69bSGarrett D'Amore 	if (cm == NULL)
52*163bd69bSGarrett D'Amore 		return (NULL);
53*163bd69bSGarrett D'Amore 	cm->cm_root = NULL;
54*163bd69bSGarrett D'Amore 	cm->cm_def = CM_DEF_SELF;
55*163bd69bSGarrett D'Amore 	cm->cm_havecache = false;
56*163bd69bSGarrett D'Amore 	cm->cm_min = cm->cm_max = 0;
57*163bd69bSGarrett D'Amore 	return (cm);
58*163bd69bSGarrett D'Amore }
59*163bd69bSGarrett D'Amore 
60*163bd69bSGarrett D'Amore /*
61*163bd69bSGarrett D'Amore  * cmap_add --
62*163bd69bSGarrett D'Amore  *	Add a mapping from "from" to "to" to the map.
63*163bd69bSGarrett D'Amore  */
64*163bd69bSGarrett D'Amore bool
cmap_add(struct cmap * cm,wint_t from,wint_t to)65*163bd69bSGarrett D'Amore cmap_add(struct cmap *cm, wint_t from, wint_t to)
66*163bd69bSGarrett D'Amore {
67*163bd69bSGarrett D'Amore 	struct cmapnode *cmn, *ncmn;
68*163bd69bSGarrett D'Amore 
69*163bd69bSGarrett D'Amore 	cm->cm_havecache = false;
70*163bd69bSGarrett D'Amore 
71*163bd69bSGarrett D'Amore 	if (cm->cm_root == NULL) {
72*163bd69bSGarrett D'Amore 		cmn = malloc(sizeof (*cmn));
73*163bd69bSGarrett D'Amore 		if (cmn == NULL)
74*163bd69bSGarrett D'Amore 			return (false);
75*163bd69bSGarrett D'Amore 		cmn->cmn_from = from;
76*163bd69bSGarrett D'Amore 		cmn->cmn_to = to;
77*163bd69bSGarrett D'Amore 		cmn->cmn_left = cmn->cmn_right = NULL;
78*163bd69bSGarrett D'Amore 		cm->cm_root = cmn;
79*163bd69bSGarrett D'Amore 		cm->cm_min = cm->cm_max = from;
80*163bd69bSGarrett D'Amore 		return (true);
81*163bd69bSGarrett D'Amore 	}
82*163bd69bSGarrett D'Amore 
83*163bd69bSGarrett D'Amore 	cmn = cm->cm_root = cmap_splay(cm->cm_root, from);
84*163bd69bSGarrett D'Amore 
85*163bd69bSGarrett D'Amore 	if (cmn->cmn_from == from) {
86*163bd69bSGarrett D'Amore 		cmn->cmn_to = to;
87*163bd69bSGarrett D'Amore 		return (true);
88*163bd69bSGarrett D'Amore 	}
89*163bd69bSGarrett D'Amore 
90*163bd69bSGarrett D'Amore 	ncmn = malloc(sizeof (*ncmn));
91*163bd69bSGarrett D'Amore 	if (ncmn == NULL)
92*163bd69bSGarrett D'Amore 		return (false);
93*163bd69bSGarrett D'Amore 	ncmn->cmn_from = from;
94*163bd69bSGarrett D'Amore 	ncmn->cmn_to = to;
95*163bd69bSGarrett D'Amore 	if (from < cmn->cmn_from) {
96*163bd69bSGarrett D'Amore 		ncmn->cmn_left = cmn->cmn_left;
97*163bd69bSGarrett D'Amore 		ncmn->cmn_right = cmn;
98*163bd69bSGarrett D'Amore 		cmn->cmn_left = NULL;
99*163bd69bSGarrett D'Amore 	} else {
100*163bd69bSGarrett D'Amore 		ncmn->cmn_right = cmn->cmn_right;
101*163bd69bSGarrett D'Amore 		ncmn->cmn_left = cmn;
102*163bd69bSGarrett D'Amore 		cmn->cmn_right = NULL;
103*163bd69bSGarrett D'Amore 	}
104*163bd69bSGarrett D'Amore 	if (from < cm->cm_min)
105*163bd69bSGarrett D'Amore 		cm->cm_min = from;
106*163bd69bSGarrett D'Amore 	if (from > cm->cm_max)
107*163bd69bSGarrett D'Amore 		cm->cm_max = from;
108*163bd69bSGarrett D'Amore 	cm->cm_root = ncmn;
109*163bd69bSGarrett D'Amore 
110*163bd69bSGarrett D'Amore 	return (true);
111*163bd69bSGarrett D'Amore }
112*163bd69bSGarrett D'Amore 
113*163bd69bSGarrett D'Amore /*
114*163bd69bSGarrett D'Amore  * cmap_lookup_hard --
115*163bd69bSGarrett D'Amore  *	Look up the mapping for a character without using the cache.
116*163bd69bSGarrett D'Amore  */
117*163bd69bSGarrett D'Amore wint_t
cmap_lookup_hard(struct cmap * cm,wint_t ch)118*163bd69bSGarrett D'Amore cmap_lookup_hard(struct cmap *cm, wint_t ch)
119*163bd69bSGarrett D'Amore {
120*163bd69bSGarrett D'Amore 
121*163bd69bSGarrett D'Amore 	if (cm->cm_root != NULL) {
122*163bd69bSGarrett D'Amore 		cm->cm_root = cmap_splay(cm->cm_root, ch);
123*163bd69bSGarrett D'Amore 		if (cm->cm_root->cmn_from == ch)
124*163bd69bSGarrett D'Amore 			return (cm->cm_root->cmn_to);
125*163bd69bSGarrett D'Amore 	}
126*163bd69bSGarrett D'Amore 	return (cm->cm_def == CM_DEF_SELF ? ch : cm->cm_def);
127*163bd69bSGarrett D'Amore }
128*163bd69bSGarrett D'Amore 
129*163bd69bSGarrett D'Amore /*
130*163bd69bSGarrett D'Amore  * cmap_cache --
131*163bd69bSGarrett D'Amore  *	Update the cache.
132*163bd69bSGarrett D'Amore  */
133*163bd69bSGarrett D'Amore void
cmap_cache(struct cmap * cm)134*163bd69bSGarrett D'Amore cmap_cache(struct cmap *cm)
135*163bd69bSGarrett D'Amore {
136*163bd69bSGarrett D'Amore 	wint_t ch;
137*163bd69bSGarrett D'Amore 
138*163bd69bSGarrett D'Amore 	for (ch = 0; ch < CM_CACHE_SIZE; ch++)
139*163bd69bSGarrett D'Amore 		cm->cm_cache[ch] = cmap_lookup_hard(cm, ch);
140*163bd69bSGarrett D'Amore 
141*163bd69bSGarrett D'Amore 	cm->cm_havecache = true;
142*163bd69bSGarrett D'Amore }
143*163bd69bSGarrett D'Amore 
144*163bd69bSGarrett D'Amore /*
145*163bd69bSGarrett D'Amore  * cmap_default --
146*163bd69bSGarrett D'Amore  *	Change the value that characters without mappings map to, and
147*163bd69bSGarrett D'Amore  *	return the old value. The special character value CM_MAP_SELF
148*163bd69bSGarrett D'Amore  *	means characters map to themselves.
149*163bd69bSGarrett D'Amore  */
150*163bd69bSGarrett D'Amore wint_t
cmap_default(struct cmap * cm,wint_t def)151*163bd69bSGarrett D'Amore cmap_default(struct cmap *cm, wint_t def)
152*163bd69bSGarrett D'Amore {
153*163bd69bSGarrett D'Amore 	wint_t old;
154*163bd69bSGarrett D'Amore 
155*163bd69bSGarrett D'Amore 	old = cm->cm_def;
156*163bd69bSGarrett D'Amore 	cm->cm_def = def;
157*163bd69bSGarrett D'Amore 	cm->cm_havecache = false;
158*163bd69bSGarrett D'Amore 	return (old);
159*163bd69bSGarrett D'Amore }
160*163bd69bSGarrett D'Amore 
161*163bd69bSGarrett D'Amore static struct cmapnode *
cmap_splay(struct cmapnode * t,wint_t ch)162*163bd69bSGarrett D'Amore cmap_splay(struct cmapnode *t, wint_t ch)
163*163bd69bSGarrett D'Amore {
164*163bd69bSGarrett D'Amore 	struct cmapnode N, *l, *r, *y;
165*163bd69bSGarrett D'Amore 
166*163bd69bSGarrett D'Amore 	/*
167*163bd69bSGarrett D'Amore 	 * Based on public domain code from Sleator.
168*163bd69bSGarrett D'Amore 	 */
169*163bd69bSGarrett D'Amore 
170*163bd69bSGarrett D'Amore 	assert(t != NULL);
171*163bd69bSGarrett D'Amore 
172*163bd69bSGarrett D'Amore 	N.cmn_left = N.cmn_right = NULL;
173*163bd69bSGarrett D'Amore 	l = r = &N;
174*163bd69bSGarrett D'Amore 	for (;;) {
175*163bd69bSGarrett D'Amore 		if (ch < t->cmn_from) {
176*163bd69bSGarrett D'Amore 			if (t->cmn_left != NULL &&
177*163bd69bSGarrett D'Amore 			    ch < t->cmn_left->cmn_from) {
178*163bd69bSGarrett D'Amore 				y = t->cmn_left;
179*163bd69bSGarrett D'Amore 				t->cmn_left = y->cmn_right;
180*163bd69bSGarrett D'Amore 				y->cmn_right = t;
181*163bd69bSGarrett D'Amore 				t = y;
182*163bd69bSGarrett D'Amore 			}
183*163bd69bSGarrett D'Amore 			if (t->cmn_left == NULL)
184*163bd69bSGarrett D'Amore 				break;
185*163bd69bSGarrett D'Amore 			r->cmn_left = t;
186*163bd69bSGarrett D'Amore 			r = t;
187*163bd69bSGarrett D'Amore 			t = t->cmn_left;
188*163bd69bSGarrett D'Amore 		} else if (ch > t->cmn_from) {
189*163bd69bSGarrett D'Amore 			if (t->cmn_right != NULL &&
190*163bd69bSGarrett D'Amore 			    ch > t->cmn_right->cmn_from) {
191*163bd69bSGarrett D'Amore 				y = t->cmn_right;
192*163bd69bSGarrett D'Amore 				t->cmn_right = y->cmn_left;
193*163bd69bSGarrett D'Amore 				y->cmn_left = t;
194*163bd69bSGarrett D'Amore 				t = y;
195*163bd69bSGarrett D'Amore 			}
196*163bd69bSGarrett D'Amore 			if (t->cmn_right == NULL)
197*163bd69bSGarrett D'Amore 				break;
198*163bd69bSGarrett D'Amore 			l->cmn_right = t;
199*163bd69bSGarrett D'Amore 			l = t;
200*163bd69bSGarrett D'Amore 			t = t->cmn_right;
201*163bd69bSGarrett D'Amore 		} else
202*163bd69bSGarrett D'Amore 			break;
203*163bd69bSGarrett D'Amore 	}
204*163bd69bSGarrett D'Amore 	l->cmn_right = t->cmn_left;
205*163bd69bSGarrett D'Amore 	r->cmn_left = t->cmn_right;
206*163bd69bSGarrett D'Amore 	t->cmn_left = N.cmn_right;
207*163bd69bSGarrett D'Amore 	t->cmn_right = N.cmn_left;
208*163bd69bSGarrett D'Amore 	return (t);
209*163bd69bSGarrett D'Amore }
210