Commit 2df6d05a9586e1405e9b5c37af03e2cf7102202f
1 parent
08afb50c
Exists in
master
First commit
Showing
1 changed file
with
116 additions
and
0 deletions
Show diff stats
... | ... | @@ -0,0 +1,116 @@ |
1 | +; $Id: //depot/Release/ENVI53_IDL85/idl/idldir/lib/obsolete/str_sep.pro#1 $ | |
2 | +; | |
3 | +; Copyright (c) 1992-2015, Exelis Visual Information Solutions, Inc and | |
4 | +; CreaSo Creative Software Systems GmbH. | |
5 | +; All rights reserved. Unauthorized reproduction prohibited. | |
6 | +;+ | |
7 | +; NAME: | |
8 | +; STR_SEP | |
9 | +; | |
10 | +; PURPOSE: | |
11 | +; This routine cuts a string into pieces which are separated by the | |
12 | +; separator string. | |
13 | +; CATEGORY: | |
14 | +; String processing. | |
15 | +; CALLING SEQUENCE: | |
16 | +; arr = STR_SEP(str, separator) | |
17 | +; | |
18 | +; INPUTS: | |
19 | +; str - The string to be separated. | |
20 | +; separator - The separator. | |
21 | +; | |
22 | +; KEYWORDS: | |
23 | +; ESC = escape character. Only valid if separator is a single character. | |
24 | +; Characters following the escape character are treated | |
25 | +; literally and not interpreted as separators. | |
26 | +; For example, if the separator is a comma, | |
27 | +; and the escape character is a backslash, the character | |
28 | +; sequence 'a\,b' is a single field containing the characters | |
29 | +; 'a,b'. | |
30 | +; REMOVE_ALL = if set, remove all blanks from fields. | |
31 | +; TRIM = if set, remove only leading and trailing blanks from fields. | |
32 | +; | |
33 | +; OUTPUT: | |
34 | +; An array of strings as function value. | |
35 | +; | |
36 | +; COMMON BLOCKS: | |
37 | +; None | |
38 | +; | |
39 | +; SIDE EFFECTS: | |
40 | +; No known side effects. | |
41 | +; | |
42 | +; RESTRICTIONS: | |
43 | +; None. | |
44 | +; | |
45 | +; EXAMPLE: | |
46 | +; array = STR_SEP ("ulib.usca.test", ".") | |
47 | +; | |
48 | +; MODIFICATION HISTORY: | |
49 | +; July 1992, AH, CreaSo Created. | |
50 | +; December, 1994, DMS, RSI Added TRIM and REMOVE_ALL. | |
51 | +;- | |
52 | +function STR_SEP, str, separator, REMOVE_ALL = remove_all, TRIM = trim, ESC=esc | |
53 | + | |
54 | + | |
55 | +ON_ERROR, 2 | |
56 | +if n_params() ne 2 then message,'Wrong number of arguments.' | |
57 | + | |
58 | +spos = 0L | |
59 | +if n_elements(esc) gt 0 then begin ;Check for escape character? | |
60 | + if strpos(str, esc) lt 0 then goto, no_esc ;None in string, use fast case | |
61 | + besc = (byte(esc))[0] | |
62 | + bsep = (byte(separator))[0] | |
63 | + new = bytarr(strlen(str)+1) | |
64 | + new[0] = byte(str) | |
65 | + j = 0L | |
66 | + for i=0L, n_elements(new)-2 do begin | |
67 | + if new[i] eq besc then begin | |
68 | + new[j] = new[i+1] | |
69 | + i = i + 1 | |
70 | + endif else if new[i] eq bsep then new[j] = 1b $ ;Change seps to 1b char | |
71 | + else new[j] = new[i] | |
72 | + j = j + 1 | |
73 | + endfor | |
74 | + new = string(new[0:j-1]) | |
75 | + w = where(byte(new) eq 1b, count) ;where seps are... | |
76 | + arr = strarr(count+1) | |
77 | + for i=0L, count-1 do begin | |
78 | + arr[i] = strmid(new, spos, w[i]-spos) | |
79 | + spos = w[i] + 1 | |
80 | + endfor | |
81 | + arr[count] = strmid(new, spos, strlen(str)) ;Last element | |
82 | + goto, done | |
83 | + endif ;esc | |
84 | + | |
85 | +no_esc: | |
86 | +if strlen(separator) le 1 then begin ;Single character separator? | |
87 | + w = where(byte(str) eq (byte(separator))[0], count) ;where seps are... | |
88 | + arr = strarr(count+1) | |
89 | + for i=0, count-1 do begin | |
90 | + arr[i] = strmid(str, spos, w[i]-spos) | |
91 | + spos = w[i] + 1 | |
92 | + endfor | |
93 | + arr[count] = strmid(str, spos, strlen(str)) ;Last element | |
94 | +endif else begin ;Multi character separator.... | |
95 | + n = 0L ; Determine number of seperators in string. | |
96 | + repeat begin | |
97 | + pos = strpos (str, separator, spos) | |
98 | + spos = pos + strlen(separator) | |
99 | + n = n+1 | |
100 | + endrep until pos eq -1 | |
101 | + | |
102 | + arr = strarr(n) ; Create result array | |
103 | + spos = 0L | |
104 | + for i=0L, n-1 do begin ; Separate substrings | |
105 | + pos = strpos (str, separator, spos) | |
106 | + if pos ge 0 then arr[i] = strmid (str, spos, pos-spos) $ | |
107 | + else arr[i] = strmid(str, spos, strlen(str)) | |
108 | + spos = pos+strlen(separator) | |
109 | + endfor | |
110 | +endelse | |
111 | + | |
112 | +done: | |
113 | +if keyword_set(trim) then arr = strtrim(arr,2) $ | |
114 | +else if keyword_set(remove_all) then arr = strcompress(arr, /REMOVE_ALL) | |
115 | +return, arr | |
116 | +end | ... | ... |