1 | module m_charset |
---|
2 | ! |
---|
3 | ! One-byte only, sorry |
---|
4 | ! |
---|
5 | private |
---|
6 | |
---|
7 | integer, parameter, private :: small_int = selected_int_kind(1) |
---|
8 | |
---|
9 | !-------------------------------------------------------------------------- |
---|
10 | type, public :: charset_t |
---|
11 | ! private |
---|
12 | integer(kind=small_int), dimension(0:255) :: mask |
---|
13 | end type charset_t |
---|
14 | |
---|
15 | |
---|
16 | public :: operator(.in.), operator(+) |
---|
17 | public :: assignment(=) |
---|
18 | public :: print_charset, reset_charset |
---|
19 | |
---|
20 | interface operator(.in.) |
---|
21 | module procedure belongs |
---|
22 | end interface |
---|
23 | private :: belongs |
---|
24 | |
---|
25 | interface assignment(=) |
---|
26 | module procedure set_string_to_charset, set_codes_to_charset |
---|
27 | end interface |
---|
28 | private :: set_string_to_charset, set_codes_to_charset |
---|
29 | |
---|
30 | interface operator(+) |
---|
31 | module procedure add_string_to_charset, & |
---|
32 | add_code_to_charset, add_codes_to_charset |
---|
33 | end interface |
---|
34 | private :: add_string_to_charset, add_code_to_charset, add_codes_to_charset |
---|
35 | |
---|
36 | !-------------------------------------------------------------------------- |
---|
37 | |
---|
38 | character(len=*), parameter, private :: & |
---|
39 | lowercase = "abcdefghijklmnopqrstuvwxyz", & |
---|
40 | uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", & |
---|
41 | digits = "0123456789" |
---|
42 | |
---|
43 | integer, parameter, public :: SPACE = 32 |
---|
44 | integer, parameter, public :: NEWLINE = 10 |
---|
45 | integer, parameter, public :: CARRIAGE_RETURN = 13 |
---|
46 | integer, parameter, public :: TAB = 9 |
---|
47 | |
---|
48 | type(charset_t), public :: initial_name_chars |
---|
49 | type(charset_t), public :: name_chars |
---|
50 | type(charset_t), public :: whitespace |
---|
51 | type(charset_t), public :: valid_chars |
---|
52 | type(charset_t), public :: uppercase_chars |
---|
53 | |
---|
54 | public :: setup_xml_charsets |
---|
55 | |
---|
56 | |
---|
57 | CONTAINS !========================================================== |
---|
58 | |
---|
59 | !-------------------------------------------------------------- |
---|
60 | function belongs(c,charset) result(res) |
---|
61 | character(len=1), intent(in) :: c |
---|
62 | type(charset_t), intent(in) :: charset |
---|
63 | logical :: res |
---|
64 | |
---|
65 | integer :: code |
---|
66 | |
---|
67 | code = ichar(c) |
---|
68 | res = (charset%mask(code) == 1) |
---|
69 | |
---|
70 | end function belongs |
---|
71 | |
---|
72 | !-------------------------------------------------------------- |
---|
73 | |
---|
74 | function add_string_to_charset(charset,str) result (sum) |
---|
75 | type(charset_t), intent(in) :: charset |
---|
76 | character(len=*), intent(in) :: str |
---|
77 | type(charset_t) :: sum |
---|
78 | |
---|
79 | integer :: length, code, i |
---|
80 | |
---|
81 | sum%mask = charset%mask |
---|
82 | |
---|
83 | length = len_trim(str) |
---|
84 | do i = 1, length |
---|
85 | code = ichar(str(i:i)) |
---|
86 | sum%mask(code) = 1 |
---|
87 | enddo |
---|
88 | end function add_string_to_charset |
---|
89 | |
---|
90 | !-------------------------------------------------------------- |
---|
91 | |
---|
92 | function add_code_to_charset(charset,code) result(sum) |
---|
93 | type(charset_t), intent(in) :: charset |
---|
94 | integer, intent(in) :: code |
---|
95 | type(charset_t) :: sum |
---|
96 | |
---|
97 | if ((code > 255) .or. (code < 0)) return |
---|
98 | sum%mask = charset%mask |
---|
99 | sum%mask(code) = 1 |
---|
100 | |
---|
101 | end function add_code_to_charset |
---|
102 | |
---|
103 | !-------------------------------------------------------------- |
---|
104 | function add_codes_to_charset(charset,codes) result(sum) |
---|
105 | type(charset_t), intent(in) :: charset |
---|
106 | integer, dimension(:), intent(in) :: codes |
---|
107 | type(charset_t) :: sum |
---|
108 | |
---|
109 | integer :: i |
---|
110 | |
---|
111 | sum%mask = charset%mask |
---|
112 | do i = 1, size(codes) |
---|
113 | if ((codes(i) > 255) .or. (codes(i) < 0)) cycle |
---|
114 | sum%mask(codes(i)) = 1 |
---|
115 | enddo |
---|
116 | end function add_codes_to_charset |
---|
117 | |
---|
118 | !-------------------------------------------------------------- |
---|
119 | |
---|
120 | subroutine set_string_to_charset(charset,str) |
---|
121 | type(charset_t), intent(out) :: charset |
---|
122 | character(len=*), intent(in) :: str |
---|
123 | |
---|
124 | |
---|
125 | integer :: length, code, i |
---|
126 | |
---|
127 | charset%mask = 0 |
---|
128 | |
---|
129 | length = len_trim(str) |
---|
130 | do i = 1, length |
---|
131 | code = ichar(str(i:i)) |
---|
132 | charset%mask(code) = 1 |
---|
133 | enddo |
---|
134 | |
---|
135 | end subroutine set_string_to_charset |
---|
136 | |
---|
137 | !-------------------------------------------------------------- |
---|
138 | |
---|
139 | subroutine set_codes_to_charset(charset,codes) |
---|
140 | type(charset_t), intent(out) :: charset |
---|
141 | integer, dimension(:), intent(in) :: codes |
---|
142 | |
---|
143 | integer :: i |
---|
144 | |
---|
145 | charset%mask = 0 |
---|
146 | |
---|
147 | do i = 1, size(codes) |
---|
148 | charset%mask(codes(i)) = 1 |
---|
149 | enddo |
---|
150 | |
---|
151 | end subroutine set_codes_to_charset |
---|
152 | |
---|
153 | |
---|
154 | !-------------------------------------------------------------- |
---|
155 | subroutine print_charset(charset) |
---|
156 | type(charset_t), intent(in) :: charset |
---|
157 | |
---|
158 | integer :: i |
---|
159 | |
---|
160 | do i = 0, 255 |
---|
161 | if (charset%mask(i) == 1) print *, "Code: ", i |
---|
162 | enddo |
---|
163 | end subroutine print_charset |
---|
164 | |
---|
165 | !-------------------------------------------------------------- |
---|
166 | |
---|
167 | subroutine reset_charset(charset) |
---|
168 | type(charset_t), intent(inout) :: charset |
---|
169 | |
---|
170 | integer :: i |
---|
171 | |
---|
172 | do i = 0, 255 |
---|
173 | charset%mask(i) = 0 |
---|
174 | enddo |
---|
175 | end subroutine reset_charset |
---|
176 | |
---|
177 | !-------------------------------------------------------------- |
---|
178 | |
---|
179 | !-------------------------------------------------------- |
---|
180 | subroutine setup_xml_charsets() |
---|
181 | |
---|
182 | integer :: i |
---|
183 | |
---|
184 | uppercase_chars = uppercase |
---|
185 | initial_name_chars = (lowercase // uppercase // "_:" ) |
---|
186 | name_chars = initial_name_chars + ( digits // ".-") |
---|
187 | whitespace = (/ SPACE, NEWLINE, TAB, CARRIAGE_RETURN /) |
---|
188 | |
---|
189 | valid_chars = whitespace + (/ (i, i=33,255) /) |
---|
190 | |
---|
191 | end subroutine setup_xml_charsets |
---|
192 | !-------------------------------------------------------- |
---|
193 | |
---|
194 | end module m_charset |
---|
195 | |
---|
196 | |
---|
197 | |
---|
198 | |
---|
199 | |
---|
200 | |
---|
201 | |
---|
202 | |
---|
203 | |
---|