1 | ; |
---|
2 | ;+ |
---|
3 | ; NAME: |
---|
4 | ; STRTRANS |
---|
5 | ; PURPOSE: |
---|
6 | ; Translate all occurences of one substring to another. |
---|
7 | ; CATEGORY: |
---|
8 | ; text/strings |
---|
9 | ; CALLING SEQUENCE: |
---|
10 | ; new = strtrans(oldstr,from,to,ned) |
---|
11 | ; INPUTS: |
---|
12 | ; oldstr -- string on which to operate. in |
---|
13 | ; May be an array. |
---|
14 | ; from -- substrings to be translated. May be in |
---|
15 | ; an array. |
---|
16 | ; to -- what strings in from should be in |
---|
17 | ; translated to. May be an array. |
---|
18 | ; KEYWORD PARAMETERS: |
---|
19 | ; /HELP -- Set this to print useful message and |
---|
20 | ; exit. |
---|
21 | ; OUTPUTS: |
---|
22 | ; new -- Translated string. Array if oldstr is out |
---|
23 | ; an array. |
---|
24 | ; ned -- number of substitutions performed in out |
---|
25 | ; oldstr. Array if oldstr is an array. |
---|
26 | ; COMMON BLOCKS: |
---|
27 | ; SIDE EFFECTS: |
---|
28 | ; NOTES: |
---|
29 | ; - Any of old, from, and to can be arrays. |
---|
30 | ; - from and to must have the same number of elements. |
---|
31 | ; EXAMPLE: |
---|
32 | ; inp='Many*bad!chars+in_here' |
---|
33 | ; from=['*','!','+','_'] |
---|
34 | ; to =[' ',' ',' ',' '] |
---|
35 | ; out = strtrans(inp,from,to,ned) |
---|
36 | ; Will produce out='Many bad chars in here', and set ned to 4. |
---|
37 | ; MODIFICATION HISTORY: |
---|
38 | ; $Id$ |
---|
39 | ; $Log$ |
---|
40 | ; Revision 1.1 2002/09/11 10:17:16 opalod |
---|
41 | ; Initial revision |
---|
42 | ; |
---|
43 | ; Revision 1.3 1996/06/14 20:00:27 mcraig |
---|
44 | ; Updated Copyright info. |
---|
45 | ; |
---|
46 | ; Revision 1.2 1996/05/09 00:22:17 mcraig |
---|
47 | ; Sped up significantly by using str_sep to handle the translation. No longer |
---|
48 | ; relies on routines fromother user libraries. |
---|
49 | ; |
---|
50 | ; Revision 1.1 1996/01/31 18:47:37 mcraig |
---|
51 | ; Initial revision |
---|
52 | ; |
---|
53 | ; RELEASE: |
---|
54 | ; $Name$ |
---|
55 | ; |
---|
56 | ; COPYRIGHT: |
---|
57 | ; Copyright (C) 1996 The Regents of the University of California, All |
---|
58 | ; Rights Reserved. Written by Matthew W. Craig. |
---|
59 | ; See the file COPYRIGHT for restrictions on distrubting this code. |
---|
60 | ; This code comes with absolutely NO warranty; see DISCLAIMER for details. |
---|
61 | ;- |
---|
62 | ; |
---|
63 | FUNCTION strtrans, InputString, from, to, ned, $ |
---|
64 | HELP=Help |
---|
65 | |
---|
66 | ; Bomb out to caller if error. |
---|
67 | On_error, 2 |
---|
68 | |
---|
69 | ; Offer help if we don't have at least InputString, from, and to, or |
---|
70 | ; if the user asks for it. |
---|
71 | IF (n_params() LT 3) OR keyword_set(help) THEN BEGIN |
---|
72 | offset = ' ' |
---|
73 | print, offset+'Translate all occurences of one substring to another.' |
---|
74 | print, offset+'new = strtrans(oldstr,from,to,ned)' |
---|
75 | print, offset+'Inputs:' |
---|
76 | print, offset+offset+'oldstr -- string on which to operate. in' |
---|
77 | print, offset+offset+' May be an array.' |
---|
78 | print, offset+offset+'from -- substrings to be translated. May be in' |
---|
79 | print, offset+offset+' an array.' |
---|
80 | print, offset+offset+'to -- what strings in from should be in' |
---|
81 | print, offset+offset+' translated to. May be an array.' |
---|
82 | print, offset+'Outputs:' |
---|
83 | print, offset+offset+'new -- Translated string. Array if oldstr is out' |
---|
84 | print, offset+offset+' an array.' |
---|
85 | print, offset+offset+'ned -- number of substitutions performed in out' |
---|
86 | print, offset+offset+' oldstr. Array if oldstr is an array.' |
---|
87 | print, offset+'Notes:' |
---|
88 | print, offset+offset+'- Any of old, from, and to can be arrays. ' |
---|
89 | print, offset+offset+'- from and to must have the same number of elements.' |
---|
90 | return, -1 |
---|
91 | ENDIF |
---|
92 | |
---|
93 | strn = InputString |
---|
94 | |
---|
95 | ; Check that From/To have same number of elements. RETURN if they don't. |
---|
96 | NFrom = n_elements(from) |
---|
97 | NTo = n_elements(to) |
---|
98 | IF (NFrom EQ 0) OR (NTo EQ 0) THEN return, strn |
---|
99 | IF NFrom NE NTo THEN BEGIN |
---|
100 | print,'Error: Number of elements in from/to unequal' |
---|
101 | return,-1 |
---|
102 | ENDIF |
---|
103 | |
---|
104 | ; Make sure there are no null strings in From. RETURN if there are. |
---|
105 | FromLen = strlen(From) |
---|
106 | IF (total(FromLen EQ 0) GT 0) THEN BEGIN |
---|
107 | print, 'Error: elements of From must have nonzero length.' |
---|
108 | return, -1 |
---|
109 | ENDIF |
---|
110 | |
---|
111 | NStrings = n_elements(strn) |
---|
112 | ned = lonarr(NStrings) |
---|
113 | tmpned = 0L |
---|
114 | |
---|
115 | ; Say strn='a#b#c', from='#' and to='@'. Then the approach here is to |
---|
116 | ; first split strn at all occurances of '#', then recombine the pieces |
---|
117 | ; with '@' inserted instead. Do this for all elements of strn, and |
---|
118 | ; all elements of from. |
---|
119 | FOR i = 0L, NStrings-1 DO BEGIN |
---|
120 | ned(i) = 0L |
---|
121 | FOR j=0L, NFrom-1 DO BEGIN |
---|
122 | SepStr = str_sep(strn(i), from(j)) |
---|
123 | NSubs = n_elements(SepStr) - 1 |
---|
124 | strn(i) = SepStr(0) |
---|
125 | FOR k=1L, NSubs DO strn(i) = strn(i) + To(j) + SepStr(k) |
---|
126 | ned(i) = ned(i) + NSubs |
---|
127 | ENDFOR |
---|
128 | ENDFOR |
---|
129 | |
---|
130 | return, strn |
---|
131 | END |
---|