Debian Codegraph @

README

raw
1 INTRODUCTION
2
3 This is a library for converting Unicode strings to numbers. Standard
4 functions like strtoul and strtod do this for numbers written in the usual
5 Western number system using the Indo-Arabic numerals, but they do not handle
6 other number systems. The main functions take as input a UTF-32 Unicode
7 string and compute the corresponding unsigned integer. Internal computation
8 is done using arbitrary precision arithmetic, so there is no limit on the size
9 of the integer that can be converted.
10
11 INSTALLATION
12
13 For installation of the C library, see the file INSTALL for details.
14 In general, you should be able to do:
15
16 ./configure
17 make
18 (su)
19 make install
20
21 For installation of the Tcl library, see README_TCL.
22
23 To use the Tcl library, from tclsh or wish
24 execute the command:
25
26 load libuninum.so
27
28 If the library is in the right place (a directory in
29 which tcl knows to look for it) this command will
30 succeed silently. You can then try it out by
31 giving the command:
32
33 source test.tcl
34
35 The image TclTestOutput.jpg shows what the result should
36 look like.
37
38 USAGE OF THE C API
39
40 (A) Converting Unicode strings to numbers
41
42 Although there are a variety of additional features, the basic use of the
43 library is very simple, perhaps simpler than using strtoul(3). Here is
44 the minimal code needed to convert a UTF-32 string to an unsigned integer.
45 We assume that str is a a wchar_t * containing a null-terminated UTF-32
46 string. You will also need the appropriate includes, which are discussed
47 in the more elaborate example below.
48
49 union ns_rval val;
50 unsigned long myint;
51
52 StringToInt(&val,str,NS_RETURN_ULONG,NS_ANY);
53 if(0 == uninum_err) myint = val.u;
54
55 This call to StringToInt attempts to convert the string str
56 and if successful places the result in val.u. It sets the
57 flag uninum_err to a non-zero value if an error occurs.
58 The argument NS_ANY tells StringToInt to attempt to determine
59 the number system itself. If it is unable to do so, uninum_err
60 will be set to NS_UNKNOWN_ERR.
61
62 The value of the string is returned in one of three forms.
63 One option is a string of ASCII characters containing the decimal
64 representation of the integer using the Indo-Arabic digits. This option has
65 the virtue of avoiding any possibility of overflow or truncation. The second
66 is to obtain the value as an unsigned long integer. If you are going
67 to do internal calculations, this is probably the most convenient option,
68 but some numbers (in fact, infinitely many) will not fit into an unsigned
69 long integer. The library guarantees that no overflow or truncation will occur;
70 if the number will not fit, it sets an error flag and returns 0.
71 The third option is to obtain the value as a GNU MP object of type mpz_t.
72 This is useful if you are going to do further arbitrary precision calculation.
73
74 The library assumes that the input is in UTF-32 Unicode, with two exceptions.
75 The writing systems for Klingon and Tengwar are not formally recognized by
76 the Unicode consortium. We assume the encodings registered with the Conscript
77 Registry. The encodings for Egyptian hieroglyphics and Sinhala are
78 the proposed Unicode encodings, which are not yet (as of version 5.0) official.
79
80 The basic interface to the library is the function StringToInt.
81
82 void StringToInt (union ns_rval *ReturnValue, wchar_t *s, short ReturnType, int NumberSystem);
83
84 The first argument is a pointer to a union of a string and an unsigned long:
85
86 union ns_rval {
87 char *s;
88 unsigned long u;
89 mpz_t m;
90 };
91
92 This is used to store the "return" value.
93
94 The second argument is the UTF-32 string that you wish to convert. The third argument
95 indicates whether the return value should be a string, an unsigned long integer,
96 or an object of type mpz_t. The fourth argument specifies the number system
97 expected, e.g. NS_CHINESE. The constants specifying number systems are
98 defined in nsdefs.h. Note that the mention of a number system in this file
99 does not guarantee that it is available. A few constants are defined for future use.
100 You can find out which number systems are actually available by using the
101 -l flag to numconv or by inspecting the source code.
102
103 If a string is returned, it is your job to free it.
104 If an object of type mpz_t is returned, when you are done with it
105 it is your job to remove it by calling mpz_clear (a function provided by
106 GNU MP).
107
108 StringToInt is an interface to a set of functions that each handle a single
109 writing system, e.g. ArabicToInt, DevanagariToInt, etc. These functions have the
110 same calling conventions except for the fact that they take no number
111 system argument.
112
113 The function WesternToInt assumes that the base is 10. The function
114 WesternGeneralToInt takes an additional argument specifying a base in the range [2,36].
115 It expects strings without base specifiers such as "0x" for hex. It overlaps
116 in function with strtoul(3). Its most likely use is in cases in which you want
117 to deal with numbers too large to fit into an unsigned long integer.
118
119 The auxiliary function
120
121 int StringToNumberSystem (char *);
122
123 returns a number specifier corresponding to a name such as "Chinese", or
124 NS_UNKNOWN if it does not recognize the name. The inverse function
125
126 char *NumberSystemToString (int);
127
128 is also provided.
129
130 The function:
131
132 char *ListNumberSystems(int,int);
133
134 is a generator that enumerates the number systems known to the library. Each time it
135 is called with a non-zero argument it returns another number system name. Calling it
136 with an argument of zero resets it to the beginning of the list. If the
137 second argument is 0 it generates the specific number system names that can be
138 used in either direction. If the second argument is 1 it lists the cover terms
139 (such as "Chinese") that can only be used when converting strings to numbers.
140
141 For example, the following line will print the list of supported specific number
142 systems on the standard output:
143
144 while (ds = ListNumberSystems(1,0)) printf("%s\n",ds);
145
146
147 To list both the specific number systems and the cover terms, we would write:
148
149 while (ds = ListNumberSystems(1,0)) printf("%s\n",ds);
150 ListNumberSystems(0,0); /* Reset */
151 while (ds = ListNumberSystems(1,1)) printf("%s\n",ds);
152
153 In almost all cases, it is possible to determine the number system from a single
154 string. The auxiliary function:
155
156 int GuessNumberSystem(wchar_t *);
157
158 returns a number system identifier corresponding to the number system of the string
159 it is passed. It returns NS_UNKNOWN if it does not recognize the number system
160 and NS_ALLZERO if the string consists entirely of zeroes. The number system of
161 such a string cannot be determined unambiguously since several number systems
162 previously lacking a zero have added one recently and sometimes use the same
163 glyph and codepoint. However, it is desirable to distinguish this case from
164 NS_UNKNOWN for two reasons. First, the value of such a string is determinate,
165 namely 0. Second, if you know that all of the data you are dealing with is in the
166 same number system, it is sensible to adopt different strategies in dealing
167 with the two cases. If the first item returns NS_UNKNOWN, you had might as well
168 abandon processing as you are not going to be able to deal with it. If the first
169 item returns NS_ALLZERO, you can expect to determine the number system
170 from subsequent items, most of which will most likely not consist entirely
171 of zeroes.
172
173
174 (B) Converting numbers to Unicode strings
175
176 The function that performs this conversion is:
177
178 wchar_t * IntToString(union ns_rval *n,int NumberSystemNumber, short InputType) {
179
180 where NumberSystemNumber is a numerical number system specifier defined
181 in nsdefs.h and InputType specifies whether the number passed is an unsigned
182 long, an ASCII decimal string, or an mpz_t object. The number to convert
183 is passed in the union to which n points. For example:
184
185 char *foureightyfive="485";
186 union ns_rval number;
187 wchar_t *numstr;
188
189 number->s = foureightyfive;
190 numstr = IntToString(&number,NS_MONGOLIAN,NS_TYPE_STRING);
191
192 will result in the Mongolian for 485 (U+1814 U+1818 U+1815)
193 being written into freshly allocated storage pointed at by numstr.
194 It is the caller's responsability to free this storage when
195 the string is no longer of use.
196
197 Output strings may be divided into groups. String delimitation is
198 controlled by several variables defined in uninum.h.
199
200 wchar_t Uninum_Output_Group_Separator;
201 int Uninum_Output_General_Group_Size;
202 int Uninum_Output_First_Group_Size;
203
204 The reason for the distinction between the two group size variables is
205 that in some number systems it is customary to use a group size of two
206 except for the lowest group which is of size three. This is typical in India.
207 For example, in India the number 123,456,789 will traditionally be delimited
208 12,34,56,789.
209
210 This pattern will be obtained with the settings:
211 Uninum_Output_General_Group_Size = 2;
212 Uninum_Output_First_Group_Size = 3;
213
214 The usual Western pattern will be obtained with the settings:
215 Uninum_Output_General_Group_Size = 3;
216 Uninum_Output_First_Group_Size = 3;
217
218 To prevent delimitation, set
219 Uninum_Output_General_Group_Size = 0;
220
221 The output base for Western_Lower and Western_Upper is determined by the variable
222 int Uninum_Output_Base;
223
224 The input base for the same number systems is determined by the variable
225 int Uninum_Input_Base;
226
227 Some number systems can only represent numbers up to a certain limit.
228 If you try to convert a number beyond the limit of representation,
229 IntToString will set uninun_err to NS_RANGE_ERROR. You can find out
230 what the maximum value representable by a number system is by
231 calling the function MaximumValue with the number system code as
232 argument. It returns a malloc-ed string containing the limit,
233 if any, in decimal, or the word "unlimited" if there is no limit.
234
235 Roman numerals may be generated either using M for all thousands or using
236 the units with a superscript bar. M is the default. This behaviour is controlled
237 by the variable:
238 int Uninum_Generate_Roman_With_Bar_P
239
240 In the program numconv, the flag -m triggers use of the superscript bar.
241 In the GUI NumberConverter, set the variable Generate_Roman_With_Bar_P
242 in your initialization file.
243
244 (C) Errors
245
246 The variable:
247
248 int uninum_err;
249
250 is used to report errors. It is set to zero at the beginning of every
251 call so you need not do it yourself. A non-zero value indicates an error.
252 The errors defined are:
253
254 NS_ERROR_OKAY
255 No error occurred.
256
257 NS_ERROR_BADCHARACTER
258 indicates that the string contains a character that it should not.
259 The first character that was not recognized is placed in the
260 variable uninum_badchar.
261
262 NS_ERROR_DOESNOTFIT
263 indicates that the number represented by the string does not fit into an
264 unsigned long integer.
265
266 NS_ERROR_NUMBER_SYSTEM_UNKNOWN
267 indicates that the writing system is not recognized.
268
269 NS_ERROR_BADBASE
270 WesternGeneralToInt has been called with a base outside the
271 valid range of [2,36].
272
273 NS_ERROR_NOTCONSISTENTWITHBASE
274 WesternGeneralToInt has been applied to a string that
275 contains a character not possible in the specified base.
276 (For example, if the specified base is 8, neither 8 nor 9 nor
277 any of the letters can validly appear in the string.)
278
279 NS_ERROR_OUTOFMEMORY
280 Indicates that it was not possible to allocate sufficient memory
281 when converting numbers to strings.
282
283 NS_ERROR_RANGE
284 Indicates that the number to be converted is too large to be
285 represented in the specified number system.
286
287
288 Three other ancillary functions are provided.
289
290 wchar_t *NormalizeChineseNumbers (wchar_t *s);
291
292 Replaces simplified and variant Chinese numerals with their standard, traditional
293 counterparts, which are the only ones understood by ChineseToInt. This function
294 may reallocate storage since some such replacements increase the number of
295 characters in the string. It is called automatically when ChineseToInt is
296 called via StringToInt.
297
298 wchar_t StripSeparators (wchar_t *s, wchar_t separator);
299
300 Returns a string from which the "thousands" separator specified in its
301 second argument has been stripped. Since most non-Western writing systems
302 rarely or never use such separators, it is not called automatically,
303 but you may find it useful.
304
305 char *uninum_version(void);
306
307 Returns the library version. This string should not be freed.
308
309 The number systems supported are:
310
311
312 Indic
313 Balinese
314 Bengali
315 Burmese
316 Devanagari
317 Gujarati
318 Gurmukhi
319 Kannada
320 Kharoshthi
321 Khmer
322 Lao
323 Limbu
324 Malayalam
325 New_Tai_Lue
326 Oriya
327 Sinhala
328 Tamil
329 Tamil_Place
330 Tamil_Traditional
331 Telugu
332 Thai
333 Tibetan
334
335
336 Miscellaneous Place
337 Arabic_Western
338 Common_Braille (Arabic, Dutch, English, German, Greek, Hebrew, Italian, Japanese, Korean, languages of India, Quebec, Vietnamese)
339 Ewellic_Decimal
340 Ewellic_Hexadecimal
341 French Braille (French, Czech)
342 Klingon
343 Mongolian
344 Nko
345 Osmanya
346 Perso_Arabic
347 Russian_Braille
348 Tengwar
349
350 Alphabetic systems
351 Arabic_Alphabetic
352 Armenian_Alphabetic
353 Cyrillic_Alphabetic
354 Glagolitic_Alphabetic
355 Greek_Alphabetic_Upper
356 Greek_Alphabetic_Lower
357 Hebrew
358 Hebrew_Early
359 Hebrew_Late
360 Mxedruli
361 Xucuri_Lower
362 Xucuri_Upper
363
364 European non-alphabetic
365 Hex
366 Old_Italic
367 Roman
368 Western
369
370 Additive-Repetitive
371 Egyptian
372 Old_Persian
373 Phoenician
374
375 Ethiopic
376 Aegean
377
378 Sinitic
379 Chinese_Regular_Traditional
380 Chinese_Mandarin_Regular_Traditional
381 Chinese_Regular_Place
382 Chinese_Regular_Simplified
383 Chinese_Mandarin_Regular_Simplified
384 Chinese_Legal_Traditional
385 Chinese_Mandarin_Legal_Traditional
386 Chinese_Legal_Simplified
387 Chinese_Mandarin_Legal_Simplified
388 Chinese_Suzhou
389 Chinese_Counting_Rod_Early
390 Chinese_Counting_Rod_Early_No_Zero
391 Chinese_Counting_Rod_Late
392 Chinese_Counting_Rod_Late_No_Zero
393 Japanese_Regular_Simplified
394 Japanese_Regular_Traditional
395 Japanese_Regular_Place
396 Japanese_Legal_Simplified
397 Japanese_Legal_Traditional
398 Japanese_Western_Mix
399
400 "regular" means that the characters used are those used for writing numbers
401 in most contexts. "legal" means that the characters used are those used
402 on legal documents, banknotes, and so forth.
403
404 In the case of Chinese proper, "traditional" means that the characters used
405 are those still in use outside of China and Singapore and used in those
406 countries as well prior to the 1960s. "simplified" means that the characters
407 used are those resulting from the reforms carried out in the People's Republic
408 of China and adopted in Singapore.
409
410 In the case of Japanese, "simplified" means that the characters used
411 are those currently in general use. "traditional" means that the characters
412 used are those used prior to the reforms of the 1950s.
413
414 The distinction between Chinese_Mandarin and Chinese is that Chinese_Mandarin
415 reflects the use of the morpheme liang "both" in place of yi "two"
416 preceding the powers of ten greater than 1. This usage is also found
417 in the Chaozhou (Teowchow) dialect, where liang is pronounced no.
418 Mandarin_Regular_Simplified and Mandarin_Regular_Traditional also
419 use zeroes in certain positions where other styles do not.
420
421 The principles that appear to govern the use of zero in current
422 Mandarin numbers, and which are implemented here are:
423 (a) if the coefficient of a power of ten is zero, zero is written;
424 (b) all sequences of two or more zeroes are reduced to a single zero.
425 (c) leading and trailing zeroes are omitted
426
427 Japanese differs from Chinese and Chinese_Mandarin in two respects.
428 First, the division between traditional and simplified characters is slightly
429 different. Second, Japanese omits leading 1s in contexts in which Chinese
430 does not.
431
432 In its historical form the Chinese number system is not place-based.
433 The two systems designated "place" are place-based systems isomorphic
434 to the Western system in which the digits are the Chinese digits
435 rather than the Indo-Arabic digits.
436
437 The Suzhou system, which is used primarily in markets, differs
438 from the usual Chinese number sytems both in that it is place-based
439 and in that it uses a distinct set of numerals.
440 It is incorrectly named "Hangzhou" in Unicode documentation.
441
442 The distinction between the Early and Late versions of the Chinese Counting
443 Rod system reflects the fact that the orientation of the symbols used for even-
444 and odd-numbered powers of ten was switched during the Han dynasty. The
445 "No_Zero" variants omit zero where it is omissible without ambiguity,
446 namely where both neighbors are non-zero and non-null. (In this case
447 the fact that there is an implicit zero can be discerned from the fact
448 that its neighbors have the same orientation.)
449
450 Japanese_Western_Mix denotes the system sometimes seen in Japan
451 in which components of numbers less than 10,000 are written the Western
452 way. For example, 25,000,000 may be written "2,500MAN", where MAN designates
453 the character U+4E07 "10,000".
454
455 Some logically possible combinations of features are not provided. For example,
456 the only place-based systems are use the simplified regular digits.
457 There is no reason in principle that traditional or legal digits could
458 not be used in a place-based manner, but to my knowledge this does not occur.
459 If you are aware of the use of feature combinations that I have not
460 provided, please let me know.
461
462
463
464 ----
465 The following program illustrates the use of the library. You may also
466 find it useful study the source for numconv.c, which provides a command-line
467 interface to the library.
468
469 ------------------------------------------------------------------------------------
470 #include <stdlib.h>
471 #include <stdio.h>
472 #include <wchar.h>
473 #include <gmp.h>
474
475 #include <uninum/unicode.h>
476 #include <uninum/nsdefs.h>
477 #include <uninum/uninum.h>
478
479 /* Create two UTF-32 strings */
480 wchar_t *s1=L"\x0A67\x0A69\x0A68"; /* Gurmukhi */
481 wchar_t *s2=L"\x0ED5\x0ED7\x0ED6"; /* Lao */
482
483 int
484 main(int ac, char **av) {
485 int ns;
486
487 /* This is where the "return" value will be stored */
488 union ns_rval val;
489
490 /* So that we can check whether it has changed */
491 uninum_err = 0;
492
493 /* We already know what number system this should be */
494 StringToInt(&val, /* pointer to return receiver */
495 s1, /* the string to convert */
496 NS_TYPE_STRING, /* flag requesting result as an ascii string */
497 NS_GURMUKHI); /* number system */
498
499 /* The string is in the s member of union val */
500 if(!uninum_err) printf("%s\n",val.s);
501
502 /* Pretend we don't know what number system s2 is in */
503 ns=GuessNumberSystem(s2);
504 printf("The second number system is: %s\n",NumberSystemToString(ns));
505 if(ns == NS_UNKNOWN) exit(2);
506
507 /* So that we can check whether it has changed */
508 uninum_err = 0;
509 StringToInt(&val,
510 s2,
511 NS_TYPE_ULONG, /* flag requesting result as an unsigned long int */
512 ns); /* number system value obtained from GuessNumberSystem */
513
514 /* Unsigned long is in u member of union val */
515 if(!uninum_err) printf("%u\n",val.u);
516
517 exit(0);
518 }
519