@@ -115,6 +115,78 @@ def _validate_data_input(
115115 raise GMTInvalidInput ("data must provide x, y, and z columns." )
116116
117117
118+ def _check_encoding (
119+ argstr : str ,
120+ ) -> Literal [
121+ "ascii" ,
122+ "ISOLatin1+" ,
123+ "ISO-8859-1" ,
124+ "ISO-8859-2" ,
125+ "ISO-8859-3" ,
126+ "ISO-8859-4" ,
127+ "ISO-8859-5" ,
128+ "ISO-8859-6" ,
129+ "ISO-8859-7" ,
130+ "ISO-8859-8" ,
131+ "ISO-8859-9" ,
132+ "ISO-8859-10" ,
133+ "ISO-8859-11" ,
134+ "ISO-8859-13" ,
135+ "ISO-8859-14" ,
136+ "ISO-8859-15" ,
137+ "ISO-8859-16" ,
138+ ]:
139+ """
140+ Check the charset encoding of a string.
141+
142+ All characters in the string must be in the same charset encoding, otherwise the
143+ default ``ISOLatin1+`` encoding is returned. Characters in the Adobe Symbol and
144+ ZapfDingbats encodings are also checked because they're independent on the choice of
145+ encodings.
146+
147+ Parameters
148+ ----------
149+ argstr
150+ The string to be checked.
151+
152+ Returns
153+ -------
154+ encoding
155+ The encoding of the string.
156+
157+ Examples
158+ --------
159+ >>> _check_encoding("123ABC+-?!") # ASCII characters only
160+ 'ascii'
161+ >>> _check_encoding("12AB±β①②") # Characters in ISOLatin1+
162+ 'ISOLatin1+'
163+ >>> _check_encoding("12ABāáâãäåβ①②") # Characters in ISO-8859-4
164+ 'ISO-8859-4'
165+ >>> _check_encoding("12ABŒā") # Mix characters in ISOLatin1+ (Œ) and ISO-8859-4 (ā)
166+ 'ISOLatin1+'
167+ >>> _check_encoding("123AB中文") # Characters not in any charset encoding
168+ 'ISOLatin1+'
169+ """
170+ # Return "ascii" if the string only contains ASCII characters.
171+ if all (32 <= ord (c ) <= 126 for c in argstr ):
172+ return "ascii"
173+ # Loop through all supported encodings and check if all characters in the string
174+ # are in the charset of the encoding. If all characters are in the charset, return
175+ # the encoding. The ISOLatin1+ encoding is checked first because it is the default
176+ # and most common encoding.
177+ adobe_chars = set (charset ["Symbol" ].values ()) | set (
178+ charset ["ZapfDingbats" ].values ()
179+ )
180+ for encoding in ["ISOLatin1+" ] + [f"ISO-8859-{ i } " for i in range (1 , 17 )]:
181+ if encoding == "ISO-8859-12" : # ISO-8859-12 was abandoned. Skip it.
182+ continue
183+ if all (c in (set (charset [encoding ].values ()) | adobe_chars ) for c in argstr ):
184+ return encoding
185+ # Return the "ISOLatin1+" encoding if the string contains characters from multiple
186+ # charset encodings or contains characters that are not in any charset encoding.
187+ return "ISOLatin1+"
188+
189+
118190def data_kind (
119191 data : Any = None , required : bool = True
120192) -> Literal ["arg" , "file" , "geojson" , "grid" , "image" , "matrix" , "vectors" ]:
@@ -192,78 +264,6 @@ def data_kind(
192264 return kind
193265
194266
195- def _check_encoding (
196- argstr : str ,
197- ) -> Literal [
198- "ascii" ,
199- "ISOLatin1+" ,
200- "ISO-8859-1" ,
201- "ISO-8859-2" ,
202- "ISO-8859-3" ,
203- "ISO-8859-4" ,
204- "ISO-8859-5" ,
205- "ISO-8859-6" ,
206- "ISO-8859-7" ,
207- "ISO-8859-8" ,
208- "ISO-8859-9" ,
209- "ISO-8859-10" ,
210- "ISO-8859-11" ,
211- "ISO-8859-13" ,
212- "ISO-8859-14" ,
213- "ISO-8859-15" ,
214- "ISO-8859-16" ,
215- ]:
216- """
217- Check the charset encoding of a string.
218-
219- All characters in the string must be in the same charset encoding, otherwise the
220- default ``ISOLatin1+`` encoding is returned. Characters in the Adobe Symbol and
221- ZapfDingbats encodings are also checked because they're independent on the choice of
222- encodings.
223-
224- Parameters
225- ----------
226- argstr
227- The string to be checked.
228-
229- Returns
230- -------
231- encoding
232- The encoding of the string.
233-
234- Examples
235- --------
236- >>> _check_encoding("123ABC+-?!") # ASCII characters only
237- 'ascii'
238- >>> _check_encoding("12AB±β①②") # Characters in ISOLatin1+
239- 'ISOLatin1+'
240- >>> _check_encoding("12ABāáâãäåβ①②") # Characters in ISO-8859-4
241- 'ISO-8859-4'
242- >>> _check_encoding("12ABŒā") # Mix characters in ISOLatin1+ (Œ) and ISO-8859-4 (ā)
243- 'ISOLatin1+'
244- >>> _check_encoding("123AB中文") # Characters not in any charset encoding
245- 'ISOLatin1+'
246- """
247- # Return "ascii" if the string only contains ASCII characters.
248- if all (32 <= ord (c ) <= 126 for c in argstr ):
249- return "ascii"
250- # Loop through all supported encodings and check if all characters in the string
251- # are in the charset of the encoding. If all characters are in the charset, return
252- # the encoding. The ISOLatin1+ encoding is checked first because it is the default
253- # and most common encoding.
254- adobe_chars = set (charset ["Symbol" ].values ()) | set (
255- charset ["ZapfDingbats" ].values ()
256- )
257- for encoding in ["ISOLatin1+" ] + [f"ISO-8859-{ i } " for i in range (1 , 17 )]:
258- if encoding == "ISO-8859-12" : # ISO-8859-12 was abandoned. Skip it.
259- continue
260- if all (c in (set (charset [encoding ].values ()) | adobe_chars ) for c in argstr ):
261- return encoding
262- # Return the "ISOLatin1+" encoding if the string contains characters from multiple
263- # charset encodings or contains characters that are not in any charset encoding.
264- return "ISOLatin1+"
265-
266-
267267def non_ascii_to_octal (
268268 argstr : str ,
269269 encoding : Literal [
0 commit comments