/*---------------------------------------------------------------------------*/ local _ctoupper; local _ctolower; /* DOCUMENT _ctoupper _ctolower Arrays to convert char to upper/lowercase letters. */ _ctolower= _ctoupper= char(indgen(0:255)); _ctoupper(1+'a':1+'z')= _ctoupper(1+'A':1+'Z'); _ctolower(1+'A':1+'Z')= _ctolower(1+'a':1+'z'); func chartolower(c) { return _ctolower(1+c); } func chartoupper(c) { return _ctoupper(1+c); } /* DOCUMENT cp= chartolower(c); cp= chartoupper(c); Convert an array of char to lower/upper case letters. */ func strtolower(s) {return strtranslate(s, _ctolower); } func strtoupper(s) {return strtranslate(s, _ctoupper); } /* DOCUMENT sp= strtolower(s); sp= strtoupper(s); Convert a string or an array of strings to lower/upper case letters. SEE ALSO: strtranslate, strtrtable. */ /*---------------------------------------------------------------------------*/ func strtranslate(s, tr) /* DOCUMENT sp= strtranslate(s, tr); Convert a string or an array of strings given a translation table TR. TR must be an array of 256 char (this is not checked). SEE ALSO: strtolower, strtoupper, strtrtable. */ { d= dimsof(s); if (d(1) == 0) return string(&tr(1+*pointer(s))); r= array(string, d); n= numberof(s); for (i=1; i<=n; i++) r(i)= string(&tr(1+*pointer(s(i)))); return r; } /*---------------------------------------------------------------------------*/ func strtrtable(in, out, &tr) /* DOCUMENT tr= strtrtable(in, out); -or- strtrtable, in, out, tr; Create or modify translation table TR so that characters that belongs to IN array will produce corresponding characters in OUT array. IN and OUT must be conformable arrays of char's. SEE ALSO: strtranslate, strtolower, strtoupper. */ { if (is_void(tr)) tr= char(indgen(0:255)); tr(in+1)= char(out+1); return tr; } /*---------------------------------------------------------------------------*/ func strtrimleft(s) { return strloop(s, _strtrimleft); } func strtrimright(s) { return strloop(s, _strtrimright); } func strtrim(s) { return strloop(s, _strtrim); } /* DOCUMENT sp= strtrim(s); sp= strtrimleft(s); sp= strtrimright(s); Remove leading and or trailing spaces from a string or from an array of strings. SEE ALSO: strloop, strtok and strpart. */ /*---------------------------------------------------------------------------*/ func strloop(s, op) /* DOCUMENT sp= strloop(s, op); Return the result of applying string operation OP to each strings of string array S. OP is a function that accept and return a scalar string. STRLOOP works around Yorick's distinction between scalars and arrays and failure to convert arrays of strings into arrays of chars (because strings may not have the same length). SEE ALSO: strtrim. */ { d= dimsof(s); if (d(1) == 0) return op(s); r= array(string, d); n= numberof(s); for (i=1; i<=n; i++) r(i)= op(s(i)); return r; } /*---------------------------------------------------------------------------*/ local _cnotspace; /* DOCUMENT _cnotspace Arrays to check whether chars are spaces or not. */ _cnotspace= array(1n, 256); _cnotspace(1+[' ', '\t', '\n', '\r', '\v', '\f'])= 0; func _strtrim(s) /* DOCUMENT sp= _strtrim(s); Scalar version of STRTRIM. */ { c= *pointer(s); nsp= _cnotspace(1+c); i= is_array((i= where(nsp))) ? i(1) : 1; j= is_array((j= where(nsp*(c!='\0')))) ? j(0) : -1; return strpart(s, i:j); } func _strtrimleft(s) /* DOCUMENT sp= _strtrimleft(s); Scalar version of STRTRIMLEFT. */ { i= where(_cnotspace(1+*pointer(s))); return (is_array(i) ? strpart(s, i(1):) : s); } func _strtrimright(s) /* DOCUMENT sp= _strtrimright(s); Scalar version of STRTRIMRIGHT. */ { i= *pointer(s); i= where(_cnotspace(1+i)*(i!='\0')); return (is_array(i) ? strpart(s, :i(0)) : s); }