-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmb_valid_var_name.pro
56 lines (48 loc) · 1.8 KB
/
cmb_valid_var_name.pro
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
;
;Copyright 1996-2013 United States Government as represented by the
;Administrator of the National Aeronautics and Space Administration.
;All Rights Reserved.
; This software may be used, copied, or redistributed as long as it is not
; sold and this copyright notice is reproduced on each copy made. This
; routine is provided as is without any express or implied warranties
; whatsoever.
;
function cmb_valid_var_name,as, notvalid=notvalid,filename=filename,ivalid = ivalid
;Check if variable names in string array 'as' are valid IDL variable names
;if not modify 'as' to make it valid by replacing invalid characters with '_'.
asv = as
for irec=0,n_elements(as)-1 do begin
a = as[irec]
a = cmb_unichar2hex(a,character_type) ;kludge for unicode SAB 8/13/2021
a = strtrim(a,2)
;a = strlowcase(a)
a0 = (byte('a'))(0)
z0 = (byte('z'))(0)
au = (byte('A'))(0)
zu = (byte('Z'))(0)
zero = (byte('0'))(0)
nine = (byte('9'))(0)
singlequote = (byte("'"))(0)
doublequote = (byte('"'))(0)
underscore = (byte('_'))(0)
null = (byte(''))(0)
;notvalid = [(byte(' '))(0), (byte('+'))(0),(byte('-'))(0), byte('!@#$%^&*()[]-+=?,.<>')]
if n_elements(notvalid) eq 0 then notvalid = byte(' !@#$%^&*()[]-+=?,.<>/\|:;"''')
if keyword_set(filename) then notvalid = byte(' !@#$%^&*()[]+=?,<>')
b = byte(a)
if ( (b[0] lt a0 or b[0] gt z0) $
and (b[0] lt au or b[0] gt zu) ) $
and (b[0] ge zero and b[0] le nine) $
and (b[0] ne singlequote and b[0] ne doublequote) then b = [(byte('z'))(0),b]
for i=0,n_elements(notvalid)-1 do begin
ii=where(b eq notvalid[i])
if ii[0] ne -1 then b[ii] = underscore ; underscore
endfor
asv[irec] = string(b)
others = strlowcase(['Ne','Eq','lt','ge','le','gt'])
for i=0,N_ELEMENTS(others)-1 do begin
if strlowcase(asv[irec]) eq others[i] then asv[irec] = '_' + asv[irec]
endfor
endfor ; irec
return,asv
end