Lines Matching refs:tr
27 * tr
31 "[-?\n@(#)$Id: tr (AT&T Research) 2010-09-08 $\n]"
33 "[+NAME?tr - translate, squeeze, and/or delete characters]"
134 nextchar(register Tr_t* tr)
144 * tr.count>0 when tr.type==1 string contains x*count
147 if (tr->count)
149 if (tr->count > 0)
150 tr->count--;
151 return tr->prev;
155 * tr.last>=0 when string contains char class
159 if (tr->last >= 0)
161 while (++tr->prev <= tr->last)
162 if (!tr->isit || (*tr->isit)(tr->prev))
163 return (!tr->type || !tr->convert) ? tr->prev : tr->convert == 'l' ? tolower(tr->prev) : toupper(tr->prev);
164 tr->last = -1;
165 tr->hold = tr->next + 1;
167 switch (c = *tr->next++)
170 tr->next--;
171 c = tr->level ? -2 : tr->type && !tr->truncate ? tr->prev : -1;
174 c = chresc((char*)tr->next - 1, (char**)&tr->next);
177 switch (*tr->next)
181 if (tr->convert)
183 c = *(tr->next + 1);
184 if (tr->convert == c || tr->type && !tr->position)
186 c = *tr->next;
189 else if (!strncmp((char*)tr->next, ":lower:", 7) || !strncmp((char*)tr->next, ":upper:", 7))
191 f = tr->isit;
192 tr->convert = c;
193 c = tr->next - tr->base;
194 if (!tr->type)
195 tr->position = c;
196 else if (tr->position != c)
200 if (!(tr->isit = regclass((char*)tr->next, (char**)&e)))
203 tr->isit = f;
207 tr->next = e;
209 tr->isit = f;
210 tr->prev = -1;
211 tr->last = UCHAR_MAX + 1;
212 return nextchar(tr);
215 if ((q = regcollate((char*)tr->next, (char**)&e, buf, sizeof(buf), &wc)) >= 0)
217 tr->next = e;
223 if (*(e = tr->next + 1))
230 if (!tr->level)
232 tr->level++;
233 c = nextchar(tr);
234 if (*tr->next == '*')
236 e = tr->next + 1;
237 if (!(tr->count = (int)strtol((char*)tr->next + 1, (char**)&tr->next, 0)) && tr->next == e)
239 if (tr->type == 0)
241 tr->count = -1;
243 if (*tr->next++ != ']')
245 if (tr->count < 0)
248 * tr->src chars total
249 * tr->dst chars so far
255 peek = *tr;
260 tr->count = tr->src - peek.dst;
262 else if (tr->count > (1<<CHAR_BIT))
263 tr->count = (1<<CHAR_BIT);
264 if (!tr->count)
266 tr->count--;
267 tr->level--;
274 if (tr->prev >= 0 && tr->next != tr->hold && *tr->next)
276 c = tr->prev;
277 tr->last = nextchar(tr);
278 if (c > tr->last)
280 tr->prev = c;
285 if (tr->level > 0 && tr->next > tr->base + 2)
287 tr->level--;
288 c = nextchar(tr);
292 return tr->prev = c;
296 * return a tr handle for <src,dst>
302 register Tr_t* tr;
309 if (!(tr = newof(0, Tr_t, 1, 0)))
321 tr->convert = '?';
324 tr->truncate = flags & TR_TRUNCATE;
329 tr->code[n] = n;
333 setchar(tr, src, 0);
334 while ((c = nextchar(tr)) >=0 && n < elementsof(set))
336 tr->code[c] |= HITBIT;
345 tr->src = n;
349 if (!(tr->code[n] & HITBIT))
351 tr->src = c;
353 if (tr->convert == '?')
354 tr->convert = 0;
355 setchar(tr, dst, 1);
356 for (tr->dst = 0; tr->dst < tr->src; tr->dst++)
358 c = set[tr->dst];
360 tr->code[c] |= DELBIT;
363 if ((x = nextchar(tr)) >= 0)
366 error(-1, "dst %d '%c' => '%c'", tr->dst, c, x);
368 tr->code[c] = x | squeeze;
372 else if (tr->truncate)
374 while (tr->dst < tr->src)
376 c = set[tr->dst++];
377 tr->code[c] = c | squeeze;
385 tr->code[c] = x | squeeze;
390 tr->truncate = 1;
391 for (tr->dst = 0; (x = nextchar(tr)) >= 0; tr->dst++)
392 if (!(tr->code[x] & DELBIT))
395 error(-1, "dst %d '%c'", tr->dst, x);
397 tr->code[x] = x | ONEBIT;
402 return tr;
404 error(2, "%s: invalid %s string", tr->base, typename[tr->type]);
405 free(tr);
410 * close a tr handle
414 trclose(Tr_t* tr)
416 free(tr);
420 * tr each char of ip and put results to op
425 trcopy(Tr_t* tr, Sfio_t* ip, Sfio_t* op, ssize_t ncopy)
429 register int* code = tr->code;
506 Tr_t* tr;
538 if (tr = tropen((unsigned char*)argv[0], (unsigned char*)argv[0] ? (unsigned char*)argv[1] : (unsigned char*)0, flags))
540 trcopy(tr, sfstdin, sfstdout, SF_UNBOUND);
541 trclose(tr);