~ubuntu-branches/ubuntu/saucy/pyicu/saucy

1 by Bernd Zeimetz
Import upstream version 0.8
1
/* ====================================================================
2
 * Copyright (c) 2004-2006 Open Source Applications Foundation.
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining a
5
 * copy of this software and associated documentation files (the "Software"),
6
 * to deal in the Software without restriction, including without limitation
7
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
 * and/or sell copies of the Software, and to permit persons to whom the
9
 * Software is furnished to do so, subject to the following conditions: 
10
 *
11
 * The above copyright notice and this permission notice shall be included
12
 * in all copies or substantial portions of the Software. 
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20
 * DEALINGS IN THE SOFTWARE.
21
 * ====================================================================
22
 */
23
24
#include "common.h"
25
#include "structmember.h"
26
27
#include "bases.h"
28
#include "locale.h"
29
#include "iterators.h"
30
#include "macros.h"
31
32
33
/* UObject */
34
35
static void t_uobject_dealloc(t_uobject *self);
36
static PyObject *t_uobject_new(PyTypeObject *type,
37
                               PyObject *args, PyObject *kwds);
38
39
static PyObject *t_uobject_richcmp(t_uobject *, PyObject *o2, int op);
40
static PyObject *t_uobject_str(t_uobject *self);
41
static PyObject *t_uobject_repr(t_uobject *self);
42
static PyObject *t_uobject_getDynamicClassID(t_uobject *self);
43
static PyObject *t_uobject__getOwned(t_uobject *self, void *data);
44
45
static PyMemberDef t_uobject_members[] = {
46
    { NULL, 0, 0, 0, NULL }
47
};
48
49
static PyMethodDef t_uobject_methods[] = {
50
    DECLARE_METHOD(t_uobject, getDynamicClassID, METH_NOARGS),
51
    { NULL, NULL, 0, NULL }
52
};
53
54
static PyGetSetDef t_uobject_properties[] = {
55
    { "owned", (getter) t_uobject__getOwned, NULL, "", NULL },
56
    { NULL, NULL, NULL, NULL, NULL }
57
};
58
59
PyTypeObject UObjectType = {
60
    PyObject_HEAD_INIT(NULL)
61
    0,                                   /* ob_size */
62
    "PyICU.UObject",                     /* tp_name */
63
    sizeof(t_uobject),                   /* tp_basicsize */
64
    0,                                   /* tp_itemsize */
65
    (destructor)t_uobject_dealloc,       /* tp_dealloc */
66
    0,                                   /* tp_print */
67
    0,                                   /* tp_getattr */
68
    0,                                   /* tp_setattr */
69
    0,                                   /* tp_compare */
70
    (reprfunc)t_uobject_repr,            /* tp_repr */
71
    0,                                   /* tp_as_number */
72
    0,                                   /* tp_as_sequence */
73
    0,                                   /* tp_as_mapping */
74
    0,                                   /* tp_hash  */
75
    0,                                   /* tp_call */
76
    (reprfunc)t_uobject_str,             /* tp_str */
77
    0,                                   /* tp_getattro */
78
    0,                                   /* tp_setattro */
79
    0,                                   /* tp_as_buffer */
80
    (Py_TPFLAGS_DEFAULT |
81
     Py_TPFLAGS_BASETYPE),               /* tp_flags */
82
    "t_uobject objects",                 /* tp_doc */
83
    0,                                   /* tp_traverse */
84
    0,                                   /* tp_clear */
85
    (richcmpfunc)t_uobject_richcmp,      /* tp_richcompare */
86
    0,                                   /* tp_weaklistoffset */
87
    0,                                   /* tp_iter */
88
    0,                                   /* tp_iternext */
89
    t_uobject_methods,                   /* tp_methods */
90
    t_uobject_members,                   /* tp_members */
91
    t_uobject_properties,                /* tp_getset */
92
    0,                                   /* tp_base */
93
    0,                                   /* tp_dict */
94
    0,                                   /* tp_descr_get */
95
    0,                                   /* tp_descr_set */
96
    0,                                   /* tp_dictoffset */
97
    (initproc)abstract_init,             /* tp_init */
98
    0,                                   /* tp_alloc */
99
    (newfunc)t_uobject_new,              /* tp_new */
100
};
101
102
PyObject *wrap_UObject(UObject *object, int flags)
103
{
104
    if (object)
105
    {
106
        if (UnicodeString::getStaticClassID() ==
107
            object->getDynamicClassID())
108
            return PyUnicode_FromUnicodeString((UnicodeString *) object);
109
110
        t_uobject *self = (t_uobject *) UObjectType.tp_alloc(&UObjectType, 0);
111
        if (self)
112
        {
113
            self->object = object;
114
            self->flags = flags;
115
        }
116
117
        return (PyObject *) self;
118
    }
119
120
    Py_RETURN_NONE;
121
}
122
123
static PyObject *t_uobject__getOwned(t_uobject *self, void *data)
124
{
125
    int b = self->flags & T_OWNED;
126
    Py_RETURN_BOOL(b);
127
}
128
129
/* Replaceable */
130
131
class t_replaceable : public _wrapper {
132
public:
133
    Replaceable *object;
134
};
135
136
static PyObject *t_replaceable_length(t_replaceable *self);
137
static PyObject *t_replaceable_charAt(t_replaceable *self, PyObject *arg);
138
static PyObject *t_replaceable_char32At(t_replaceable *self, PyObject *arg);
139
static PyObject *t_replaceable_hasMetaData(t_replaceable *self);
140
141
static PyMethodDef t_replaceable_methods[] = {
142
    DECLARE_METHOD(t_replaceable, length, METH_NOARGS),
143
    DECLARE_METHOD(t_replaceable, charAt, METH_O),
144
    DECLARE_METHOD(t_replaceable, char32At, METH_O),
145
    DECLARE_METHOD(t_replaceable, hasMetaData, METH_NOARGS),
146
    { NULL, NULL, 0, NULL }
147
};
148
149
DECLARE_TYPE(Replaceable, t_replaceable, UObject, Replaceable, abstract_init);
150
151
/* UnicodeString */
152
153
class t_unicodestring : public _wrapper {
154
public:
155
    UnicodeString *object;
156
};
157
158
static int t_unicodestring_init(t_unicodestring *self,
159
                                PyObject *args, PyObject *kwds);
160
static PyObject *t_unicodestring_getAvailableStandards(PyTypeObject *type);
161
static PyObject *t_unicodestring_getAvailableEncodings(PyTypeObject *type,
162
                                                       PyObject *args);
163
static PyObject *t_unicodestring_getStandardEncoding(PyTypeObject *type,
164
                                                     PyObject *args);
165
static PyObject *t_unicodestring_append(t_unicodestring *self, PyObject *args);
166
static PyObject *t_unicodestring_compare(t_unicodestring *self, PyObject *args);
167
static PyObject *t_unicodestring_compareBetween(t_unicodestring *self,
168
                                                PyObject *args);
169
static PyObject *t_unicodestring_compareCodePointOrder(t_unicodestring *self,
170
                                                       PyObject *args);
171
static PyObject *t_unicodestring_compareCodePointOrderBetween(t_unicodestring *self, PyObject *args);
172
static PyObject *t_unicodestring_caseCompare(t_unicodestring *self,
173
                                             PyObject *args);
174
static PyObject *t_unicodestring_caseCompareBetween(t_unicodestring *self,
175
                                                    PyObject *args);
176
static PyObject *t_unicodestring_startsWith(t_unicodestring *self,
177
                                            PyObject *args);
178
static PyObject *t_unicodestring_endsWith(t_unicodestring *self,
179
                                          PyObject *args);
180
#define t_unicodestring_startswith t_unicodestring_startsWith
181
#define t_unicodestring_endswith t_unicodestring_endsWith
182
static PyObject *t_unicodestring_indexOf(t_unicodestring *self, PyObject *args);
183
static PyObject *t_unicodestring_lastIndexOf(t_unicodestring *self,
184
                                             PyObject *args);
185
static PyObject *t_unicodestring_trim(t_unicodestring *self);
186
static PyObject *t_unicodestring_reverse(t_unicodestring *self);
187
static PyObject *t_unicodestring_toUpper(t_unicodestring *self, PyObject *args);
188
static PyObject *t_unicodestring_toLower(t_unicodestring *self, PyObject *args);
189
static PyObject *t_unicodestring_toTitle(t_unicodestring *self, PyObject *args);
190
static PyObject *t_unicodestring_foldCase(t_unicodestring *self,
191
                                          PyObject *args);
192
static PyObject *t_unicodestring_isBogus(t_unicodestring *self);
193
static PyObject *t_unicodestring_encode(t_unicodestring *self, PyObject *arg);
194
static PyObject *t_unicodestring_idna_toASCII(t_unicodestring *self,
195
                                              PyObject *args);
196
static PyObject *t_unicodestring_idna_toUnicode(t_unicodestring *self,
197
                                                PyObject *args);
198
static PyObject *t_unicodestring_idna_IDNtoASCII(t_unicodestring *self,
199
                                                 PyObject *args);
200
static PyObject *t_unicodestring_idna_IDNtoUnicode(t_unicodestring *self,
201
                                                   PyObject *args);
202
static PyObject *t_unicodestring_idna_compare(t_unicodestring *self,
203
                                              PyObject *args);
204
205
static PyMethodDef t_unicodestring_methods[] = {
206
    DECLARE_METHOD(t_unicodestring, getAvailableStandards, METH_NOARGS | METH_CLASS),
207
    DECLARE_METHOD(t_unicodestring, getAvailableEncodings, METH_VARARGS | METH_CLASS),
208
    DECLARE_METHOD(t_unicodestring, getStandardEncoding, METH_VARARGS | METH_CLASS),
209
    DECLARE_METHOD(t_unicodestring, append, METH_VARARGS),
210
    DECLARE_METHOD(t_unicodestring, compare, METH_VARARGS),
211
    DECLARE_METHOD(t_unicodestring, compareBetween, METH_VARARGS),
212
    DECLARE_METHOD(t_unicodestring, compareCodePointOrder, METH_VARARGS),
213
    DECLARE_METHOD(t_unicodestring, compareCodePointOrderBetween, METH_VARARGS),
214
    DECLARE_METHOD(t_unicodestring, caseCompare, METH_VARARGS),
215
    DECLARE_METHOD(t_unicodestring, caseCompareBetween, METH_VARARGS),
216
    DECLARE_METHOD(t_unicodestring, startsWith, METH_VARARGS),
217
    DECLARE_METHOD(t_unicodestring, endsWith, METH_VARARGS),
218
    DECLARE_METHOD(t_unicodestring, startswith, METH_VARARGS),
219
    DECLARE_METHOD(t_unicodestring, endswith, METH_VARARGS),
220
    DECLARE_METHOD(t_unicodestring, indexOf, METH_VARARGS),
221
    DECLARE_METHOD(t_unicodestring, lastIndexOf, METH_VARARGS),
222
    DECLARE_METHOD(t_unicodestring, trim, METH_NOARGS),
223
    DECLARE_METHOD(t_unicodestring, reverse, METH_NOARGS),
224
    DECLARE_METHOD(t_unicodestring, toUpper, METH_VARARGS),
225
    DECLARE_METHOD(t_unicodestring, toLower, METH_VARARGS),
226
    DECLARE_METHOD(t_unicodestring, toTitle, METH_VARARGS),
227
    DECLARE_METHOD(t_unicodestring, foldCase, METH_VARARGS),
228
    DECLARE_METHOD(t_unicodestring, isBogus, METH_NOARGS),
229
    DECLARE_METHOD(t_unicodestring, encode, METH_O),
230
    DECLARE_METHOD(t_unicodestring, idna_toASCII, METH_VARARGS),
231
    DECLARE_METHOD(t_unicodestring, idna_toUnicode, METH_VARARGS),
232
    DECLARE_METHOD(t_unicodestring, idna_IDNtoASCII, METH_VARARGS),
233
    DECLARE_METHOD(t_unicodestring, idna_IDNtoUnicode, METH_VARARGS),
234
    DECLARE_METHOD(t_unicodestring, idna_compare, METH_VARARGS),
235
    { NULL, NULL, 0, NULL }
236
};
237
238
DECLARE_TYPE(UnicodeString, t_unicodestring, Replaceable, UnicodeString,
239
             t_unicodestring_init);
240
241
/* Formattable */
242
243
class t_formattable : public _wrapper {
244
public:
245
    Formattable *object;
246
};
247
248
static int t_formattable_init(t_formattable *self,
249
                              PyObject *args, PyObject *kwds);
250
static PyObject *t_formattable_isNumeric(t_formattable *self);
251
static PyObject *t_formattable_getType(t_formattable *self);
252
static PyObject *t_formattable_getDouble(t_formattable *self);
253
static PyObject *t_formattable_getLong(t_formattable *self);
254
static PyObject *t_formattable_getInt64(t_formattable *self);
255
static PyObject *t_formattable_getDate(t_formattable *self);
256
static PyObject *t_formattable_getString(t_formattable *self, PyObject *args);
257
static PyObject *t_formattable_setDouble(t_formattable *self, PyObject *arg);
258
static PyObject *t_formattable_setLong(t_formattable *self, PyObject *arg);
259
static PyObject *t_formattable_setInt64(t_formattable *self, PyObject *arg);
260
static PyObject *t_formattable_setDate(t_formattable *self, PyObject *arg);
261
static PyObject *t_formattable_setString(t_formattable *self, PyObject *arg);
262
263
static PyMethodDef t_formattable_methods[] = {
264
    DECLARE_METHOD(t_formattable, isNumeric, METH_NOARGS),
265
    DECLARE_METHOD(t_formattable, getType, METH_NOARGS),
266
    DECLARE_METHOD(t_formattable, getDouble, METH_NOARGS),
267
    DECLARE_METHOD(t_formattable, getLong, METH_NOARGS),
268
    DECLARE_METHOD(t_formattable, getInt64, METH_NOARGS),
269
    DECLARE_METHOD(t_formattable, getDate, METH_NOARGS),
270
    DECLARE_METHOD(t_formattable, getString, METH_VARARGS),
271
    DECLARE_METHOD(t_formattable, setDouble, METH_O),
272
    DECLARE_METHOD(t_formattable, setLong, METH_O),
273
    DECLARE_METHOD(t_formattable, setInt64, METH_O),
274
    DECLARE_METHOD(t_formattable, setDate, METH_O),
275
    DECLARE_METHOD(t_formattable, setString, METH_O),
276
    { NULL, NULL, 0, NULL }
277
};
278
279
DECLARE_TYPE(Formattable, t_formattable, UObject, Formattable,
280
             t_formattable_init);
281
282
PyObject *wrap_Formattable(Formattable &formattable)
283
{
284
    return wrap_Formattable(new Formattable(formattable), T_OWNED);
285
}
286
287
288
/* MeasureUnit */
289
290
class t_measureunit : public _wrapper {
291
public:
292
    MeasureUnit *object;
293
};
294
295
static PyMethodDef t_measureunit_methods[] = {
296
    { NULL, NULL, 0, NULL }
297
};
298
299
DECLARE_TYPE(MeasureUnit, t_measureunit, UObject, MeasureUnit, abstract_init);
300
301
/* Measure */
302
303
class t_measure : public _wrapper {
304
public:
305
    Measure *object;
306
};
307
308
static PyObject *t_measure_getNumber(t_measure *self);
309
static PyObject *t_measure_getUnit(t_measure *self);
310
311
static PyMethodDef t_measure_methods[] = {
312
    DECLARE_METHOD(t_measure, getNumber, METH_NOARGS),
313
    DECLARE_METHOD(t_measure, getUnit, METH_NOARGS),
314
    { NULL, NULL, 0, NULL }
315
};
316
317
DECLARE_TYPE(Measure, t_measure, UObject, Measure, abstract_init);
318
319
/* CurrencyUnit */
320
321
class t_currencyunit : public _wrapper {
322
public:
323
    CurrencyUnit *object;
324
};
325
326
static int t_currencyunit_init(t_currencyunit *self,
327
                               PyObject *args, PyObject *kwds);
328
static PyObject *t_currencyunit_getISOCurrency(t_currencyunit *self);
329
330
static PyMethodDef t_currencyunit_methods[] = {
331
    DECLARE_METHOD(t_currencyunit, getISOCurrency, METH_NOARGS),
332
    { NULL, NULL, 0, NULL }
333
};
334
335
DECLARE_TYPE(CurrencyUnit, t_currencyunit, MeasureUnit, CurrencyUnit,
336
             t_currencyunit_init);
337
338
/* CurrencyAmount */
339
340
class t_currencyamount : public _wrapper {
341
public:
342
    CurrencyAmount *object;
343
};
344
345
static int t_currencyamount_init(t_currencyamount *self,
346
                                 PyObject *args, PyObject *kwds);
347
static PyObject *t_currencyamount_getCurrency(t_currencyamount *self);
348
349
static PyMethodDef t_currencyamount_methods[] = {
350
    DECLARE_METHOD(t_currencyamount, getCurrency, METH_NOARGS),
351
    { NULL, NULL, 0, NULL }
352
};
353
354
DECLARE_TYPE(CurrencyAmount, t_currencyamount, Measure, CurrencyAmount,
355
             t_currencyamount_init);
356
357
/* StringEnumeration */
358
359
class t_stringenumeration : public _wrapper {
360
public:
361
    StringEnumeration *object;
362
};
363
364
static PyObject *t_stringenumeration_count(t_stringenumeration *self);
365
static PyObject *t_stringenumeration_reset(t_stringenumeration *self);
366
static PyObject *t_stringenumeration_next(t_stringenumeration *self);
367
static PyObject *t_stringenumeration_unext(t_stringenumeration *self);
368
static PyObject *t_stringenumeration_snext(t_stringenumeration *self);
369
370
static PyMethodDef t_stringenumeration_methods[] = {
371
    DECLARE_METHOD(t_stringenumeration, count, METH_NOARGS),
372
    DECLARE_METHOD(t_stringenumeration, reset, METH_NOARGS),
373
    DECLARE_METHOD(t_stringenumeration, next, METH_NOARGS),
374
    DECLARE_METHOD(t_stringenumeration, unext, METH_NOARGS),
375
    DECLARE_METHOD(t_stringenumeration, snext, METH_NOARGS),
376
    { NULL, NULL, 0, NULL }
377
};
378
379
DECLARE_TYPE(StringEnumeration, t_stringenumeration, UObject,
380
             StringEnumeration, abstract_init);
381
382
383
/* UObject */
384
385
static void t_uobject_dealloc(t_uobject *self)
386
{
387
    if (self->object)
388
    {
389
        if (self->flags & T_OWNED)
390
            delete self->object;
391
392
        self->object = NULL;
393
    }
394
395
    self->ob_type->tp_free((PyObject *) self);
396
}
397
398
static PyObject *t_uobject_new(PyTypeObject *type,
399
                               PyObject *args, PyObject *kwds)
400
{
401
    t_uobject *self = (t_uobject *) type->tp_alloc(type, 0);
402
403
    if (self)
404
    {
405
        self->object = NULL;
406
        self->flags = 0;
407
    }
408
409
    return (PyObject *) self;
410
}
411
412
static PyObject *t_uobject_richcmp(t_uobject *self, PyObject *arg, int op)
413
{
414
    int b = 0;
415
416
    switch (op) {
417
      case Py_EQ:
418
      case Py_NE:
419
        if (PyObject_TypeCheck(arg, &UObjectType))
420
            b = self->object == ((t_uobject *) arg)->object;
421
        if (op == Py_EQ)
422
            Py_RETURN_BOOL(b);
423
        Py_RETURN_BOOL(!b);
424
      case Py_LT:
425
        PyErr_SetString(PyExc_NotImplementedError, "<");
426
        return NULL;
427
      case Py_LE:
428
        PyErr_SetString(PyExc_NotImplementedError, "<=");
429
        return NULL;
430
      case Py_GT:
431
        PyErr_SetString(PyExc_NotImplementedError, ">");
432
        return NULL;
433
      case Py_GE:
434
        PyErr_SetString(PyExc_NotImplementedError, ">=");
435
        return NULL;
436
    }
437
438
    return NULL;
439
}
440
441
static PyObject *t_uobject_str(t_uobject *self)
442
{
443
    if (self->object)
444
    {
445
        char buf[32];
446
        sprintf(buf, "0x%lx",
447
		(unsigned long) self->object->getDynamicClassID());
448
        return PyString_FromString(buf);
449
    }
450
451
    return PyString_FromString("<null>");
452
}
453
454
static PyObject *t_uobject_repr(t_uobject *self)
455
{
456
    PyObject *name = PyObject_GetAttrString((PyObject *) self->ob_type,
457
                                            "__name__");
458
    PyObject *str = self->ob_type->tp_str((PyObject *) self);
459
#if PY_VERSION_HEX < 0x02040000
460
    PyObject *args = Py_BuildValue("(OO)", name, str);
461
#else
462
    PyObject *args = PyTuple_Pack(2, name, str);
463
#endif
464
    PyObject *format = PyString_FromString("<%s: %s>");
465
    PyObject *repr = PyString_Format(format, args);
466
467
    Py_DECREF(name);
468
    Py_DECREF(str);
469
    Py_DECREF(args);
470
    Py_DECREF(format);
471
472
    return repr;
473
}
474
475
static PyObject *t_uobject_getDynamicClassID(t_uobject *self)
476
{
477
    return PyInt_FromLong((long) self->object->getDynamicClassID());
478
}
479
480
481
/* Replaceable */
482
483
static PyObject *t_replaceable_length(t_replaceable *self)
484
{
485
    return PyInt_FromLong(self->object->length());
486
}
487
488
static PyObject *t_replaceable_charAt(t_replaceable *self, PyObject *arg)
489
{
490
    int32_t i;
491
492
    if (!parseArg(arg, "i", &i))
493
    {
494
        if (i >= 0 && self->object->length())
495
            return PyInt_FromLong(self->object->charAt(i));
496
        else
497
        {
498
            PyErr_SetObject(PyExc_IndexError, arg);
499
            return NULL;
500
        }
501
    }
502
503
    return PyErr_SetArgsError((PyObject *) self, "charAt", arg);
504
}
505
506
static PyObject *t_replaceable_char32At(t_replaceable *self, PyObject *arg)
507
{
508
    int32_t i;
509
510
    if (!parseArg(arg, "i", &i))
511
    {
512
        if (i >= 0 && self->object->length())
513
            return PyInt_FromLong(self->object->char32At(i));
514
        else
515
        {
516
            PyErr_SetObject(PyExc_IndexError, arg);
517
            return NULL;
518
        }
519
    }
520
521
    return PyErr_SetArgsError((PyObject *) self, "char32At", arg);
522
}
523
524
static PyObject *t_replaceable_hasMetaData(t_replaceable *self)
525
{
526
    int b = self->object->hasMetaData();
527
    Py_RETURN_BOOL(b);
528
}
529
530
531
/* UnicodeString */
532
533
static int t_unicodestring_init(t_unicodestring *self,
534
                                PyObject *args, PyObject *kwds)
535
{
536
    UnicodeString *u;
537
    PyObject *obj;
538
    char *encoding, *mode;
539
    int i;
540
541
    switch (PyTuple_Size(args)) {
542
      case 0:
543
        self->object = new UnicodeString();
544
        self->flags = T_OWNED;
545
        break;
546
      case 1:
547
        if (!parseArgs(args, "s", &u))
548
        {
549
            self->object = u;
550
            self->flags = T_OWNED;
551
            break;
552
        }
553
        if (!parseArgs(args, "U", &u))
554
        {
555
            self->object = new UnicodeString(*u);
556
            self->flags = T_OWNED;
557
            break;
558
        }
559
        if (!parseArgs(args, "i", &i))
560
        {
561
            self->object = new UnicodeString((UChar32) i);
562
            self->flags = T_OWNED;
563
            break;
564
        }
565
        PyErr_SetArgsError((PyObject *) self, "__init__", args);
566
        return -1;
567
      case 2:
568
        if (!parseArgs(args, "Cc", &obj, &encoding))
569
        {
570
            UnicodeString u;
571
572
            try {
573
                PyObject_AsUnicodeString(obj, encoding, "strict", u);
574
                self->object = new UnicodeString(u);
575
                self->flags = T_OWNED;
576
            } catch (ICUException e) {
577
                e.reportError();
578
                return -1;
579
            }
580
            break;
581
        }
582
        PyErr_SetArgsError((PyObject *) self, "__init__", args);
583
        return -1;
584
      case 3:
585
        if (!parseArgs(args, "Ccc", &obj, &encoding, &mode))
586
        {
587
            UnicodeString u;
588
589
            try {
590
                PyObject_AsUnicodeString(obj, encoding, mode, u);
591
                self->object = new UnicodeString(u);
592
                self->flags = T_OWNED;
593
            } catch (ICUException e) {
594
                e.reportError();
595
                return -1;
596
            }
597
            break;
598
        }
599
        PyErr_SetArgsError((PyObject *) self, "__init__", args);
600
        return -1;
601
      default:
602
        PyErr_SetArgsError((PyObject *) self, "__init__", args);
603
        return -1;
604
    }
605
        
606
    if (self->object)
607
        return 0;
608
609
    return -1;
610
}
611
612
static PyObject *t_unicodestring_getAvailableStandards(PyTypeObject *type)
613
{
614
    UErrorCode status = U_ZERO_ERROR;
615
    int count = ucnv_countStandards();
616
    PyObject *list = PyList_New(count);
617
618
    for (int i = 0; i < count; i++) {
619
        const char *name = ucnv_getStandard(i, &status);
620
        PyList_SetItem(list, i, PyString_FromString(name));
621
    }
622
623
    return list;
624
}
625
626
static PyObject *t_unicodestring_getAvailableEncodings(PyTypeObject *type,
627
                                                       PyObject *args)
628
{
629
    char *standard = NULL;
630
631
    switch (PyTuple_Size(args)) {
632
      case 0:
633
        standard = NULL;
634
        break;
635
      case 1:
636
        if (!parseArgs(args, "c", &standard))
637
            break;
638
      default:
639
        return PyErr_SetArgsError(type, "getAvailableEncodings", args);
640
    }
641
        
642
    int count = ucnv_countAvailable();
643
    PyObject *list = PyList_New(0);
644
645
    for (int i = 0; i < count; i++) {
646
        const char *name = ucnv_getAvailableName(i);
647
648
        if (standard)
649
        {
650
            UErrorCode status = U_ZERO_ERROR;
651
            name = ucnv_getStandardName(name, standard, &status);
652
        }
653
654
        if (name)
655
            PyList_Append(list, PyString_FromString(name));
656
    }
657
658
    return list;
659
}
660
661
static PyObject *t_unicodestring_getStandardEncoding(PyTypeObject *type,
662
                                                     PyObject *args)
663
{
664
    char *name, *standard;
665
666
    if (!parseArgs(args, "cc", &name, &standard))
667
    {
668
        UErrorCode status = U_ZERO_ERROR;
669
        const char *standardName =
670
            ucnv_getStandardName(name, standard, &status);
671
672
        if (standardName)
673
            return PyString_FromString(standardName);
674
675
        Py_RETURN_NONE;
676
    }
677
678
    return PyErr_SetArgsError(type, "getStandardEncodings", args);
679
}
680
681
static int verifyStart(int &start, int size)
682
{
683
    if (start < 0)
684
    {
685
        start += size;
686
        if (start < 0)
687
            return -1;
688
    }
689
690
    return 0;
691
}
692
693
static int verifyStartLen(int &start, int &len, int size)
694
{
695
    if (start < 0)
696
    {
697
        start += size;
698
        if (start < 0)
699
            return -1;
700
    }
701
702
    if (len < 0)
703
        len = 0;
704
    else if (len > size - start)
705
        len = size - start;
706
707
    return 0;
708
}
709
710
static int verifyStartEnd(int &start, int &end, int len)
711
{
712
    if (start < 0)
713
    {
714
        start += len;
715
        if (start < 0)
716
            return -1;
717
    }
718
    else if (start > len)
719
        start = len;
720
721
    if (end < 0)
722
    {
723
        end += len;
724
        if (end < 0)
725
            return -1;
726
    }
727
    else if (end > len)
728
        end = len;
729
730
    return 0;
731
}
732
733
static PyObject *t_unicodestring_append(t_unicodestring *self, PyObject *args)
734
{
735
    UnicodeString *u;
736
    UnicodeString _u;
737
    int i, start, len;
738
739
    switch (PyTuple_Size(args)) {
740
      case 1:
741
        if (!parseArgs(args, "S", &u, &_u))
742
        {
743
            self->object->append(*u);
744
745
            Py_INCREF(self);
746
            return (PyObject *) self;
747
        }
748
        if (!parseArgs(args, "i", &i))
749
        {
750
            if (sizeof(Py_UNICODE) == sizeof(UChar))
751
                self->object->append((UChar) i);
752
            else
753
                self->object->append((UChar32) i);
754
755
            Py_INCREF(self);
756
            return (PyObject *) self;
757
        }
758
        break;
759
      case 3:
760
        if (!parseArgs(args, "Sii", &u, &_u, &start, &len))
761
        {
762
            if (!verifyStartLen(start, len, u->length()))
763
            {
764
                self->object->append(*u, start, len);
765
766
                Py_INCREF(self);
767
                return (PyObject *) self;
768
            }
769
            PyErr_SetObject(PyExc_IndexError, args);
770
            return NULL;
771
        }
772
        break;
773
    }
774
    
775
    return PyErr_SetArgsError((PyObject *) self, "append", args);
776
}
777
778
static PyObject *t_unicodestring_compare(t_unicodestring *self, PyObject *args)
779
{
780
    UnicodeString *u;
781
    UnicodeString _u;
782
    int start, len;
783
784
    switch (PyTuple_Size(args)) {
785
      case 1:
786
        if (!parseArgs(args, "S", &u, &_u))
787
        {
788
            int c = self->object->compare(*u);
789
            return PyInt_FromLong(c);
790
        }
791
        break;
792
      case 3:
793
        if (!parseArgs(args, "iiS", &start, &len, &u, &_u))
794
        {
795
            if (!verifyStartLen(start, len, u->length()))
796
            {
797
                int c = self->object->compare(start, len, *u);
798
                return PyInt_FromLong(c);
799
            }
800
            PyErr_SetObject(PyExc_IndexError, args);
801
            return NULL;
802
        }
803
        break;
804
    }
805
    
806
    return PyErr_SetArgsError((PyObject *) self, "compare", args);
807
}
808
809
static PyObject *t_unicodestring_compareBetween(t_unicodestring *self,
810
                                                PyObject *args)
811
{
812
    UnicodeString *u;
813
    UnicodeString _u;
814
    int start, end, srcStart, srcEnd;
815
816
    if (!parseArgs(args, "iiSii", &start, &end, &u, &_u, &srcStart, &srcEnd))
817
    {
818
        if (!verifyStartEnd(start, end, self->object->length()) &&
819
            !verifyStartEnd(srcStart, srcEnd, u->length()))
820
        {
821
            int c = self->object->compareBetween(start, end, *u,
822
                                                 srcStart, srcEnd);
823
            return PyInt_FromLong(c);
824
        }
825
        
826
        PyErr_SetObject(PyExc_IndexError, args);
827
        return NULL;
828
    }
829
    
830
    return PyErr_SetArgsError((PyObject *) self, "compareBetween", args);
831
}
832
833
static PyObject *t_unicodestring_compareCodePointOrder(t_unicodestring *self,
834
                                                       PyObject *args)
835
{
836
    UnicodeString *u;
837
    UnicodeString _u;
838
    int start, len;
839
840
    switch (PyTuple_Size(args)) {
841
      case 1:
842
        if (!parseArgs(args, "S", &u, &_u))
843
        {
844
            int c = self->object->compareCodePointOrder(*u);
845
            return PyInt_FromLong(c);
846
        }
847
        break;
848
      case 3:
849
        if (!parseArgs(args, "iiS", &start, &len, &u, &_u))
850
        {
851
            if (!verifyStartLen(start, len, self->object->length()))
852
            {
853
                int c = self->object->compareCodePointOrder(start, len, *u);
854
                return PyInt_FromLong(c);
855
            }
856
857
            PyErr_SetObject(PyExc_IndexError, args);
858
            return NULL;
859
        }
860
        break;
861
    }
862
    
863
    return PyErr_SetArgsError((PyObject *) self, "compareCodePointOrder", args);
864
}
865
866
static PyObject *t_unicodestring_compareCodePointOrderBetween(t_unicodestring *self, PyObject *args)
867
{
868
    UnicodeString *u;
869
    UnicodeString _u;
870
    int start, end, srcStart, srcEnd;
871
872
    if (!parseArgs(args, "iiSii", &start, &end, &u, &_u, &srcStart, &srcEnd))
873
    {
874
        if (!verifyStartEnd(start, end, self->object->length()) &&
875
            !verifyStartEnd(srcStart, srcEnd, u->length()))
876
        {
877
            int c = self->object->compareCodePointOrderBetween(start, end, *u, srcStart, srcEnd);
878
            return PyInt_FromLong(c);
879
        }
880
        
881
        PyErr_SetObject(PyExc_IndexError, args);
882
        return NULL;
883
    }
884
    
885
    return PyErr_SetArgsError((PyObject *) self, "compareCodePointOrderBetween",
886
                              args);
887
}
888
889
static PyObject *t_unicodestring_caseCompare(t_unicodestring *self,
890
                                             PyObject *args)
891
{
892
    UnicodeString *u;
893
    UnicodeString _u;
894
    int start, len, options;
895
896
    switch (PyTuple_Size(args)) {
897
      case 2:
898
        if (!parseArgs(args, "Si", &u, &_u, &options))
899
        {
900
            int c = self->object->caseCompare(*u, options);
901
            return PyInt_FromLong(c);
902
        }
903
        break;
904
      case 4:
905
        if (!parseArgs(args, "iiSi", &start, &len, &u, &_u, &options))
906
        {
907
            if (!verifyStartLen(start, len, self->object->length()))
908
            {
909
                int c = self->object->caseCompare(start, len, *u, options);
910
                return PyInt_FromLong(c);
911
            }
912
913
            PyErr_SetObject(PyExc_IndexError, args);
914
            return NULL;
915
        }
916
        break;
917
    }
918
    
919
    return PyErr_SetArgsError((PyObject *) self, "caseCompare", args);
920
}
921
922
static PyObject *t_unicodestring_caseCompareBetween(t_unicodestring *self,
923
                                                    PyObject *args)
924
{
925
    UnicodeString *u;
926
    UnicodeString _u;
927
    int start, end, srcStart, srcEnd, options;
928
929
    if (!parseArgs(args, "iiSiii", &start, &end, &u, &_u, &srcStart, &srcEnd,
930
                   &options))
931
    {
932
        if (!verifyStartEnd(start, end, self->object->length()) &&
933
            !verifyStartEnd(srcStart, srcEnd, u->length()))
934
        {
935
            int c = self->object->caseCompareBetween(start, end, *u,
936
                                                     srcStart, srcEnd, options);
937
            return PyInt_FromLong(c);
938
        }
939
        
940
        PyErr_SetObject(PyExc_IndexError, args);
941
        return NULL;
942
    }
943
    
944
    return PyErr_SetArgsError((PyObject *) self, "caseCompareBetween", args);
945
}
946
947
static PyObject *t_unicodestring_startsWith(t_unicodestring *self,
948
                                            PyObject *args)
949
{
950
    UnicodeString *u;
951
    UnicodeString _u;
952
    int start, len;
953
954
    switch (PyTuple_Size(args)) {
955
      case 1:
956
        if (!parseArgs(args, "S", &u, &_u))
957
        {
958
            int b = self->object->startsWith(*u);
959
            Py_RETURN_BOOL(b);
960
        }
961
        break;
962
      case 3:
963
        if (!parseArgs(args, "Sii", &u, &_u, &start, &len))
964
        {
965
            if (!verifyStartLen(start, len, u->length()))
966
            {
967
                int b = self->object->startsWith(*u, start, len);
968
                Py_RETURN_BOOL(b);
969
            }
970
971
            PyErr_SetObject(PyExc_IndexError, args);
972
            return NULL;
973
        }
974
        break;
975
    }
976
    
977
    return PyErr_SetArgsError((PyObject *) self, "startsWith", args);
978
}
979
980
static PyObject *t_unicodestring_endsWith(t_unicodestring *self,
981
                                          PyObject *args)
982
{
983
    UnicodeString *u;
984
    UnicodeString _u;
985
    int start, len;
986
987
    switch (PyTuple_Size(args)) {
988
      case 1:
989
        if (!parseArgs(args, "S", &u, &_u))
990
        {
991
            int b = self->object->endsWith(*u);
992
            Py_RETURN_BOOL(b);
993
        }
994
        break;
995
      case 3:
996
        if (!parseArgs(args, "Sii", &u, &_u, &start, &len))
997
        {
998
            if (!verifyStartLen(start, len, u->length()))
999
            {
1000
                int b = self->object->endsWith(*u, start, len);
1001
                Py_RETURN_BOOL(b);
1002
            }
1003
1004
            PyErr_SetObject(PyExc_IndexError, args);
1005
            return NULL;
1006
        }
1007
        break;
1008
    }
1009
    
1010
    return PyErr_SetArgsError((PyObject *) self, "endsWith", args);
1011
}
1012
1013
static PyObject *t_unicodestring_indexOf(t_unicodestring *self, PyObject *args)
1014
{
1015
    UnicodeString *u;
1016
    UnicodeString _u;
1017
    int c, start, len, srcStart, srcLen;
1018
1019
    switch (PyTuple_Size(args)) {
1020
      case 1:
1021
        if (!parseArgs(args, "S", &u, &_u))
1022
        {
1023
            int i = self->object->indexOf(*u);
1024
            return PyInt_FromLong(i);
1025
        }
1026
        if (!parseArgs(args, "i", &c))
1027
        {
1028
            int i;
1029
1030
            if (sizeof(Py_UNICODE) == sizeof(UChar))
1031
                i = self->object->indexOf((UChar) c);
1032
            else
1033
                i = self->object->indexOf((UChar32) c);
1034
1035
            return PyInt_FromLong(i);
1036
        }
1037
        break;
1038
      case 2:
1039
        if (!parseArgs(args, "Si", &u, &_u, &start))
1040
        {
1041
            if (!verifyStart(start, u->length()))
1042
            {
1043
                int i = self->object->indexOf(*u, start);
1044
                return PyInt_FromLong(i);
1045
            }
1046
            PyErr_SetObject(PyExc_IndexError, args);
1047
            return NULL;
1048
        }
1049
        if (!parseArgs(args, "ii", &c, &start))
1050
        {
1051
            if (!verifyStart(start, self->object->length()))
1052
            {
1053
                int i;
1054
1055
                if (sizeof(Py_UNICODE) == sizeof(UChar))
1056
                    i = self->object->indexOf((UChar) c, start);
1057
                else
1058
                    i = self->object->indexOf((UChar32) c, start);
1059
1060
                return PyInt_FromLong(i);
1061
            }
1062
            PyErr_SetObject(PyExc_IndexError, args);
1063
            return NULL;
1064
        }
1065
        break;
1066
      case 3:
1067
        if (!parseArgs(args, "Sii", &u, &_u, &start, &len))
1068
        {
1069
            if (!verifyStartLen(start, len, u->length()))
1070
            {
1071
                int i = self->object->indexOf(*u, start, len);
1072
                return PyInt_FromLong(i);
1073
            }
1074
            PyErr_SetObject(PyExc_IndexError, args);
1075
            return NULL;
1076
        }
1077
        if (!parseArgs(args, "iii", &c, &start, &len))
1078
        {
1079
            if (!verifyStartLen(start, len, self->object->length()))
1080
            {
1081
                int i;
1082
1083
                if (sizeof(Py_UNICODE) == sizeof(UChar))
1084
                    i = self->object->indexOf((UChar) c, start, len);
1085
                else
1086
                    i = self->object->indexOf((UChar32) c, start, len);
1087
1088
                return PyInt_FromLong(i);
1089
            }
1090
            PyErr_SetObject(PyExc_IndexError, args);
1091
            return NULL;
1092
        }
1093
        break;
1094
      case 5:
1095
        if (!parseArgs(args, "Siiii", &u, &_u,
1096
                       &srcStart, &srcLen, &start, &len))
1097
        {
1098
            if (!verifyStartLen(srcStart, srcLen, u->length()) &&
1099
                !verifyStartLen(start, len, self->object->length()))
1100
            {
1101
                int i = self->object->indexOf(*u, srcStart, srcLen, start, len);
1102
                return PyInt_FromLong(i);
1103
            }
1104
            PyErr_SetObject(PyExc_IndexError, args);
1105
            return NULL;
1106
        }
1107
    }
1108
1109
    return PyErr_SetArgsError((PyObject *) self, "indexOf", args);
1110
}
1111
1112
static PyObject *t_unicodestring_lastIndexOf(t_unicodestring *self,
1113
                                             PyObject *args)
1114
{
1115
    UnicodeString *u;
1116
    UnicodeString _u;
1117
    int c, start, len, srcStart, srcLen;
1118
1119
    switch (PyTuple_Size(args)) {
1120
      case 1:
1121
        if (!parseArgs(args, "S", &u, &_u))
1122
        {
1123
            int i = self->object->lastIndexOf(*u);
1124
            return PyInt_FromLong(i);
1125
        }
1126
        if (!parseArgs(args, "i", &c))
1127
        {
1128
            int i;
1129
1130
            if (sizeof(Py_UNICODE) == sizeof(UChar))
1131
                i = self->object->lastIndexOf((UChar) c);
1132
            else
1133
                i = self->object->lastIndexOf((UChar32) c);
1134
1135
            return PyInt_FromLong(i);
1136
        }
1137
        break;
1138
      case 2:
1139
        if (!parseArgs(args, "Si", &u, &_u, &start))
1140
        {
1141
            if (!verifyStart(start, u->length()))
1142
            {
1143
                int i = self->object->lastIndexOf(*u, start);
1144
                return PyInt_FromLong(i);
1145
            }
1146
            PyErr_SetObject(PyExc_IndexError, args);
1147
            return NULL;
1148
        }
1149
        if (!parseArgs(args, "ii", &c, &start))
1150
        {
1151
            if (!verifyStart(start, self->object->length()))
1152
            {
1153
                int i;
1154
1155
                if (sizeof(Py_UNICODE) == sizeof(UChar))
1156
                    i = self->object->lastIndexOf((UChar) c, start);
1157
                else
1158
                    i = self->object->lastIndexOf((UChar32) c, start);
1159
1160
                return PyInt_FromLong(i);
1161
            }
1162
            PyErr_SetObject(PyExc_IndexError, args);
1163
            return NULL;
1164
        }
1165
        break;
1166
      case 3:
1167
        if (!parseArgs(args, "Sii", &u, &_u, &start, &len))
1168
        {
1169
            if (!verifyStartLen(start, len, u->length()))
1170
            {
1171
                int i = self->object->lastIndexOf(*u, start, len);
1172
                return PyInt_FromLong(i);
1173
            }
1174
            PyErr_SetObject(PyExc_IndexError, args);
1175
            return NULL;
1176
        }
1177
        if (!parseArgs(args, "iii", &c, &start, &len))
1178
        {
1179
            if (!verifyStartLen(start, len, self->object->length()))
1180
            {
1181
                int i;
1182
1183
                if (sizeof(Py_UNICODE) == sizeof(UChar))
1184
                    i = self->object->lastIndexOf((UChar) c, start, len);
1185
                else
1186
                    i = self->object->lastIndexOf((UChar32) c, start, len);
1187
1188
                return PyInt_FromLong(i);
1189
            }
1190
            PyErr_SetObject(PyExc_IndexError, args);
1191
            return NULL;
1192
        }
1193
        break;
1194
      case 5:
1195
        if (!parseArgs(args, "Siiii", &u, &_u,
1196
                       &srcStart, &srcLen, &start, &len))
1197
        {
1198
            if (!verifyStartLen(srcStart, srcLen, u->length()) &&
1199
                !verifyStartLen(start, len, self->object->length()))
1200
            {
1201
                int i = self->object->lastIndexOf(*u, srcStart, srcLen,
1202
                                                  start, len);
1203
                return PyInt_FromLong(i);
1204
            }
1205
            PyErr_SetObject(PyExc_IndexError, args);
1206
            return NULL;
1207
        }
1208
    }
1209
1210
    return PyErr_SetArgsError((PyObject *) self, "lastIndexOf", args);
1211
}
1212
1213
static PyObject *t_unicodestring_trim(t_unicodestring *self)
1214
{
1215
    self->object->trim();
1216
    Py_RETURN_SELF();
1217
}
1218
1219
static PyObject *t_unicodestring_reverse(t_unicodestring *self)
1220
{
1221
    self->object->reverse();
1222
    Py_RETURN_SELF();
1223
}
1224
1225
static PyObject *t_unicodestring_toUpper(t_unicodestring *self, PyObject *args)
1226
{
1227
    Locale *locale;
1228
1229
    switch (PyTuple_Size(args)) {
1230
      case 0:
1231
        self->object->toUpper();
1232
        Py_RETURN_SELF();
1233
      case 1:
1234
        if (!parseArgs(args, "P", TYPE_CLASSID(Locale), &locale))
1235
        {
1236
            self->object->toUpper(*locale);
1237
            Py_RETURN_SELF();
1238
        }
1239
        break;
1240
    }
1241
1242
    return PyErr_SetArgsError((PyObject *) self, "toUpper", args);
1243
}
1244
1245
static PyObject *t_unicodestring_toLower(t_unicodestring *self, PyObject *args)
1246
{
1247
    Locale *locale;
1248
1249
    switch (PyTuple_Size(args)) {
1250
      case 0:
1251
        self->object->toLower();
1252
        Py_RETURN_SELF();
1253
      case 1:
1254
        if (!parseArgs(args, "P", TYPE_CLASSID(Locale), &locale))
1255
        {
1256
            self->object->toLower(*locale);
1257
            Py_RETURN_SELF();
1258
        }
1259
        break;
1260
    }
1261
1262
    return PyErr_SetArgsError((PyObject *) self, "toLower", args);
1263
}
1264
1265
static PyObject *t_unicodestring_toTitle(t_unicodestring *self, PyObject *args)
1266
{
1267
    BreakIterator *iterator;
1268
    Locale *locale;
1269
1270
    switch (PyTuple_Size(args)) {
1271
      case 1:
1272
        if (!parseArg(args, "P", TYPE_ID(BreakIterator), &iterator))
1273
        {
1274
            self->object->toTitle(iterator);
1275
            Py_RETURN_SELF();
1276
        }
1277
        break;
1278
      case 2:
1279
        if (!parseArgs(args, "PP", TYPE_ID(BreakIterator),
1280
                       TYPE_CLASSID(Locale), &iterator, &locale))
1281
        {
1282
            self->object->toTitle(iterator, *locale);
1283
            Py_RETURN_SELF();
1284
        }
1285
        break;
1286
    }
1287
1288
    return PyErr_SetArgsError((PyObject *) self, "toTitle", args);
1289
}
1290
1291
static PyObject *t_unicodestring_foldCase(t_unicodestring *self, PyObject *args)
1292
{
1293
    int i;
1294
1295
    switch (PyTuple_Size(args)) {
1296
      case 0:
1297
        self->object->foldCase(0);
1298
        Py_RETURN_SELF();
1299
      case 1:
1300
        if (!parseArgs(args, "i", &i))
1301
        {
1302
            self->object->foldCase(i);
1303
            Py_RETURN_SELF();
1304
        }
1305
        break;
1306
    }
1307
1308
    return PyErr_SetArgsError((PyObject *) self, "foldCase", args);
1309
}
1310
1311
static PyObject *t_unicodestring_isBogus(t_unicodestring *self)
1312
{
1313
    int b = self->object->isBogus();
1314
    Py_RETURN_BOOL(b);
1315
}
1316
1317
static PyObject *t_unicodestring_str(t_unicodestring *self)
1318
{
1319
    return PyUnicode_FromUnicodeString(self->object);
1320
}
1321
1322
static PyObject *t_unicodestring_repr(t_unicodestring *self)
1323
{
1324
    PyObject *name = PyObject_GetAttrString((PyObject *) self->ob_type,
1325
                                            "__name__");
1326
    PyObject *str = PyUnicode_FromUnicodeString(self->object);
1327
1328
    if (str)
1329
    {
1330
        PyObject *repr = str->ob_type->tp_repr(str);
1331
        Py_DECREF(str);
1332
        str = repr;
1333
    }
1334
    if (!str)
1335
        return NULL;
1336
    
1337
#if PY_VERSION_HEX < 0x02040000
1338
    PyObject *args = Py_BuildValue("(OO)", name, str);
1339
#else
1340
    PyObject *args = PyTuple_Pack(2, name, str);
1341
#endif
1342
    PyObject *format = PyString_FromString("<%s: %s>");
1343
    PyObject *repr = PyString_Format(format, args);
1344
1345
    Py_DECREF(name);
1346
    Py_DECREF(str);
1347
    Py_DECREF(args);
1348
    Py_DECREF(format);
1349
1350
    return repr;
1351
}
1352
1353
1354
static PyObject *t_unicodestring_encode(t_unicodestring *self, PyObject *arg)
1355
{
1356
    char *encoding;
1357
1358
    if (!parseArg(arg, "c", &encoding))
1359
    {
1360
        int len = self->object->length();
1361
        UErrorCode status = U_ZERO_ERROR;
1362
        UConverter *conv = ucnv_open(encoding, &status);
1363
        PyObject *string;
1364
        char *dest;
1365
1366
        if (U_FAILURE(status))
1367
            return ICUException(status).reportError();
1368
1369
        dest = new char[len * 4];
1370
        if (!dest)
1371
        {
1372
            ucnv_close(conv);
1373
            PyErr_SetNone(PyExc_MemoryError);
1374
            return NULL;
1375
        }
1376
1377
        len = ucnv_fromUChars(conv, dest, len * 4,
1378
                              self->object->getBuffer(), len, &status);
1379
        ucnv_close(conv);
1380
1381
        if (U_FAILURE(status))
1382
        {
1383
            delete dest;
1384
            return ICUException(status).reportError();
1385
        }
1386
1387
        string = PyString_FromStringAndSize(dest, len);
1388
        delete dest;
1389
1390
        return string;
1391
    }
1392
1393
    return PyErr_SetArgsError((PyObject *) self, "encode", arg);
1394
}
1395
1396
static PyObject *t_unicodestring_idna_toASCII(t_unicodestring *self,
1397
                                              PyObject *args)
1398
{
1399
    UErrorCode status = U_ZERO_ERROR;
1400
    UParseError parseError;
1401
    int options = UIDNA_DEFAULT;
1402
    int len = self->object->length();
1403
    UnicodeString *u;
1404
    UChar *dest;
1405
1406
    if (!PyArg_ParseTuple(args, "|i", &options))
1407
        return NULL;
1408
1409
    dest = new UChar[len * 4 + 32];
1410
    if (!dest)
1411
    {
1412
        PyErr_SetNone(PyExc_MemoryError);
1413
        return NULL;
1414
    }
1415
1416
    len = uidna_toASCII(self->object->getBuffer(), len,
1417
                        dest, len * 4 + 32, options, &parseError, &status);
1418
1419
    if (U_FAILURE(status))
1420
    {
1421
        delete dest;
1422
        return ICUException(parseError, status).reportError();
1423
    }
1424
1425
    u = new UnicodeString(dest, len);
1426
    delete dest;
1427
1428
    return wrap_UnicodeString(u, T_OWNED);
1429
}
1430
1431
static PyObject *t_unicodestring_idna_toUnicode(t_unicodestring *self,
1432
                                                PyObject *args)
1433
{
1434
    UErrorCode status = U_ZERO_ERROR;
1435
    UParseError parseError;
1436
    int options = UIDNA_DEFAULT;
1437
    int len = self->object->length();
1438
    UnicodeString *u;
1439
    UChar *dest;
1440
1441
    if (!PyArg_ParseTuple(args, "|i", &options))
1442
        return NULL;
1443
1444
    dest = new UChar[len];
1445
    if (!dest)
1446
    {
1447
        PyErr_SetNone(PyExc_MemoryError);
1448
        return NULL;
1449
    }
1450
1451
    len = uidna_toUnicode(self->object->getBuffer(), len,
1452
                          dest, len, options, &parseError, &status);
1453
1454
    if (U_FAILURE(status))
1455
    {
1456
        delete dest;
1457
        return ICUException(parseError, status).reportError();
1458
    }
1459
1460
    u = new UnicodeString(dest, len);
1461
    delete dest;
1462
1463
    return wrap_UnicodeString(u, T_OWNED);
1464
}
1465
1466
static PyObject *t_unicodestring_idna_IDNtoASCII(t_unicodestring *self,
1467
                                                 PyObject *args)
1468
{
1469
    UErrorCode status = U_ZERO_ERROR;
1470
    UParseError parseError;
1471
    int options = UIDNA_DEFAULT;
1472
    int len = self->object->length();
1473
    UnicodeString *u;
1474
    UChar *dest;
1475
1476
    if (!PyArg_ParseTuple(args, "|i", &options))
1477
        return NULL;
1478
1479
    dest = new UChar[len * 4 + 32];
1480
    if (!dest)
1481
    {
1482
        PyErr_SetNone(PyExc_MemoryError);
1483
        return NULL;
1484
    }
1485
1486
    len = uidna_IDNToASCII(self->object->getBuffer(), len,
1487
                           dest, len * 4 + 32, options, &parseError, &status);
1488
1489
    if (U_FAILURE(status))
1490
    {
1491
        delete dest;
1492
        return ICUException(parseError, status).reportError();
1493
    }
1494
1495
    u = new UnicodeString(dest, len);
1496
    delete dest;
1497
1498
    return wrap_UnicodeString(u, T_OWNED);
1499
}
1500
1501
static PyObject *t_unicodestring_idna_IDNtoUnicode(t_unicodestring *self,
1502
                                                   PyObject *args)
1503
{
1504
    UErrorCode status = U_ZERO_ERROR;
1505
    UParseError parseError;
1506
    int options = UIDNA_DEFAULT;
1507
    int len = self->object->length();
1508
    UnicodeString *u;
1509
    UChar *dest;
1510
1511
    if (!PyArg_ParseTuple(args, "|i", &options))
1512
        return NULL;
1513
1514
    dest = new UChar[len];
1515
    if (!dest)
1516
    {
1517
        PyErr_SetNone(PyExc_MemoryError);
1518
        return NULL;
1519
    }
1520
1521
    len = uidna_IDNToUnicode(self->object->getBuffer(), len,
1522
                             dest, len, options, &parseError, &status);
1523
1524
    if (U_FAILURE(status))
1525
    {
1526
        delete dest;
1527
        return ICUException(parseError, status).reportError();
1528
    }
1529
1530
    u = new UnicodeString(dest, len);
1531
    delete dest;
1532
1533
    return wrap_UnicodeString(u, T_OWNED);
1534
}
1535
1536
static PyObject *t_unicodestring_idna_compare(t_unicodestring *self,
1537
                                              PyObject *args)
1538
{
1539
    int options = UIDNA_DEFAULT;
1540
    UnicodeString *u;
1541
    UnicodeString _u;
1542
    int n;
1543
1544
    switch (PyTuple_Size(args)) {
1545
      case 1:
1546
        if (!parseArgs(args, "S", &u, &_u))
1547
        {
1548
            STATUS_CALL(n = uidna_compare(self->object->getBuffer(),
1549
                                          self->object->length(),
1550
                                          u->getBuffer(), u->length(), options,
1551
                                          &status));
1552
            return PyInt_FromLong(n);
1553
        }
1554
        break;
1555
      case 2:
1556
        if (!parseArgs(args, "Si", &u, &_u, &options))
1557
        {
1558
            STATUS_CALL(n = uidna_compare(self->object->getBuffer(),
1559
                                          self->object->length(),
1560
                                          u->getBuffer(), u->length(), options,
1561
                                          &status));
1562
            return PyInt_FromLong(n);
1563
        }
1564
        break;
1565
    }
1566
1567
    return PyErr_SetArgsError((PyObject *) self, "idna_compare", args);
1568
}
1569
1570
1571
static PyObject *t_unicodestring_richcmp(t_unicodestring *self,
1572
                                         PyObject *arg, int op)
1573
{
1574
    UnicodeString *u;
1575
    UnicodeString _u;
1576
    int b = 0;
1577
1578
    if (isUnicodeString(arg))
1579
        u = (UnicodeString *) ((t_uobject *) arg)->object;
1580
    else
1581
        try {
1582
            PyObject_AsUnicodeString(arg, _u);
1583
            u = &_u;
1584
        } catch (ICUException e) {
1585
            return e.reportError();
1586
        }
1587
        
1588
    switch (op) {
1589
      case Py_EQ:
1590
        b = *self->object == *u;
1591
        break;
1592
      case Py_NE:
1593
        b = *self->object != *u;
1594
        break;
1595
      case Py_LT:
1596
        b = *self->object < *u;
1597
        break;
1598
      case Py_LE:
1599
        b = *self->object <= *u;
1600
        break;
1601
      case Py_GT:
1602
        b = *self->object > *u;
1603
        break;
1604
      case Py_GE:
1605
        b = *self->object >= *u;
1606
        break;
1607
      default:
1608
        PyErr_SetNone(PyExc_NotImplementedError);
1609
        return NULL;
1610
    }
1611
1612
    Py_RETURN_BOOL(b);
1613
}
1614
1615
static Py_ssize_t t_unicodestring_length(t_unicodestring *self)
1616
{
1617
    return self->object->length();
1618
}
1619
1620
static PyObject *t_unicodestring_concat(t_unicodestring *self, PyObject *arg)
1621
{
1622
    UnicodeString *u;
1623
    UnicodeString _u;
1624
    int i;
1625
1626
    if (!parseArg(arg, "S", &u, &_u))
1627
    {
1628
        UnicodeString *v = new UnicodeString(*self->object);
1629
        *v += *u;
1630
        return wrap_UnicodeString(v, T_OWNED);
1631
    }
1632
1633
    if (!parseArg(arg, "i", &i))
1634
    {
1635
        UnicodeString *v = new UnicodeString(*self->object);
1636
1637
        if (sizeof(Py_UNICODE) == sizeof(UChar))
1638
            v->append((UChar) i);
1639
        else
1640
            v->append((UChar32) i);
1641
1642
        return wrap_UnicodeString(v, T_OWNED);
1643
    }
1644
1645
    return PyErr_SetArgsError((PyObject *) self, "+", arg);
1646
}
1647
1648
static PyObject *t_unicodestring_repeat(t_unicodestring *self, Py_ssize_t n)
1649
{
1650
    if (n <= 0)
1651
        return wrap_UnicodeString(new UnicodeString(), T_OWNED);
1652
    else
1653
    {
1654
        UnicodeString *u = self->object;
1655
        UnicodeString *v = new UnicodeString(u->length() * n, 0, 0);
1656
    
1657
        while (n-- > 0)
1658
            *v += *u;
1659
1660
        return wrap_UnicodeString(v, T_OWNED);
1661
    }
1662
}
1663
1664
static PyObject *t_unicodestring_item(t_unicodestring *self, int n)
1665
{
1666
    UnicodeString *u = self->object;
1667
    int len = u->length();
1668
1669
    if (n < 0)
1670
        n += len;
1671
1672
    if (n >= 0 && n < len)
1673
    {
1674
        if (sizeof(Py_UNICODE) == sizeof(UChar))
1675
        {
1676
            Py_UNICODE c = (Py_UNICODE) u->charAt(n);
1677
            return PyUnicode_FromUnicode(&c, 1);
1678
        }
1679
        else
1680
        {
1681
            Py_UNICODE c = (Py_UNICODE) u->char32At(n);
1682
            return PyUnicode_FromUnicode(&c, 1);
1683
        }
1684
    }
1685
1686
    PyErr_SetNone(PyExc_IndexError);
1687
    return NULL;
1688
}
1689
1690
static PyObject *t_unicodestring_slice(t_unicodestring *self,
1691
                                       Py_ssize_t l, Py_ssize_t h)
1692
{
1693
    UnicodeString *u = self->object;
1694
    int len = u->length();
1695
1696
    if (l < 0)
1697
        l += len;
1698
    else if (l > len)
1699
        l = len;
1700
1701
    if (h < 0)
1702
        h += len;
1703
    else if (h > len)
1704
        h = len;
1705
1706
    UnicodeString *v = new UnicodeString();
1707
1708
    if (l >= 0 && h >= 0)
1709
    {
1710
        if (h > l)
1711
            u->extract(l, h - l, *v);
1712
1713
        return wrap_UnicodeString(v, T_OWNED);
1714
    }
1715
1716
    PyErr_SetNone(PyExc_IndexError);
1717
    return NULL;
1718
}
1719
1720
static int t_unicodestring_ass_item(t_unicodestring *self,
1721
                                    Py_ssize_t n, PyObject *arg)
1722
{
1723
    UnicodeString *u = self->object;
1724
    int len = u->length();
1725
1726
    if (n < 0)
1727
        n += len;
1728
1729
    if (n >= 0 && n < len)
1730
    {
1731
        int32_t i;
1732
1733
        if (!parseArg(arg, "i", &i))
1734
        {
1735
            if (sizeof(Py_UNICODE) == sizeof(UChar))
1736
                u->replace(n, 1, (UChar) i);
1737
            else
1738
                u->replace(n, 1, (UChar32) i);
1739
1740
            return 0;
1741
        }
1742
1743
        UnicodeString *v;
1744
        UnicodeString _v;
1745
1746
        if (!parseArg(arg, "S", &v, &_v))
1747
        {
1748
            if (v->length() == 1)
1749
            {
1750
                u[n] = v[n];
1751
                return 0;
1752
            }
1753
            else
1754
            {
1755
                PyErr_SetObject(PyExc_ValueError, arg);
1756
                return -1;
1757
            }
1758
        }
1759
1760
        PyErr_SetObject(PyExc_TypeError, arg);
1761
        return -1;
1762
    }
1763
1764
    PyErr_SetNone(PyExc_IndexError);
1765
    return -1;
1766
}
1767
1768
static int t_unicodestring_ass_slice(t_unicodestring *self,
1769
                                     Py_ssize_t l, Py_ssize_t h,
1770
                                     PyObject *arg)
1771
{
1772
    UnicodeString *v;
1773
    UnicodeString _v;
1774
1775
    if (!parseArg(arg, "S", &v, &_v))
1776
    {
1777
        UnicodeString *u = self->object;
1778
        int len = u->length();
1779
1780
        if (l < 0)
1781
            l += len;
1782
        else if (l > len)
1783
            l = len;
1784
1785
        if (h < 0)
1786
            h += len;
1787
        else if (h > len)
1788
            h = len;
1789
1790
        if (h < l)
1791
            h = l;
1792
1793
        if (h >= 0 && l >= 0)
1794
        {
1795
            u->replaceBetween(l, h, *v);
1796
            return 0;
1797
        }
1798
1799
        PyErr_SetNone(PyExc_IndexError);
1800
        return -1;
1801
    }
1802
1803
    PyErr_SetObject(PyExc_TypeError, arg);
1804
    return -1;
1805
}
1806
1807
static int t_unicodestring_contains(t_unicodestring *self, PyObject *arg)
1808
{
1809
    UnicodeString *u;
1810
    UnicodeString _u;
1811
1812
    if (!parseArg(arg, "S", &u, &_u))
1813
        return self->object->indexOf(*u) == 0;
1814
1815
    PyErr_SetObject(PyExc_TypeError, arg);
1816
    return -1;
1817
}
1818
1819
static PyObject *t_unicodestring_inplace_concat(t_unicodestring *self,
1820
                                                PyObject *arg)
1821
{
1822
    UnicodeString *u;
1823
    UnicodeString _u;
1824
    int i;
1825
1826
    if (!parseArg(arg, "S", &u, &_u))
1827
    {
1828
        *self->object += *u;
1829
1830
        Py_INCREF(self);
1831
        return (PyObject *) self;
1832
    }
1833
1834
    if (!parseArg(arg, "i", &i))
1835
    {
1836
        if (sizeof(Py_UNICODE) == sizeof(UChar))
1837
            self->object->append((UChar) i);
1838
        else
1839
            self->object->append((UChar32) i);
1840
1841
        Py_INCREF(self);
1842
        return (PyObject *) self;
1843
    }
1844
1845
    return PyErr_SetArgsError((PyObject *) self, "+=", arg);
1846
}
1847
1848
static PyObject *t_unicodestring_inplace_repeat(t_unicodestring *self,
1849
                                                Py_ssize_t n)
1850
{
1851
    if (n <= 0)
1852
        self->object->remove();
1853
    else if (n > 1)
1854
    {
1855
        UnicodeString v = *self->object;
1856
        while (n-- > 1)
1857
            *self->object += v;
1858
    }
1859
1860
    Py_INCREF(self);
1861
    return (PyObject *) self;
1862
}
1863
1864
static PySequenceMethods t_unicodestring_as_sequence = {
1865
    (lenfunc) t_unicodestring_length,                   /* sq_length */
1866
    (binaryfunc) t_unicodestring_concat,                /* sq_concat */
1867
    (ssizeargfunc) t_unicodestring_repeat,              /* sq_repeat */
1868
    (ssizeargfunc) t_unicodestring_item,                /* sq_item */
1869
    (ssizessizeargfunc) t_unicodestring_slice,          /* sq_slice */
1870
    (ssizeobjargproc) t_unicodestring_ass_item,         /* sq_ass_item */
1871
    (ssizessizeobjargproc) t_unicodestring_ass_slice,   /* sq_ass_slice */
1872
    (objobjproc) t_unicodestring_contains,              /* sq_contains */
1873
    (binaryfunc) t_unicodestring_inplace_concat,        /* sq_inplace_concat */
1874
    (ssizeargfunc) t_unicodestring_inplace_repeat,      /* sq_inplace_repeat */
1875
};
1876
1877
1878
/* Formattable */
1879
1880
static int t_formattable_init(t_formattable *self,
1881
                              PyObject *args, PyObject *kwds)
1882
{
1883
    UDate date;
1884
    Formattable::ISDATE flag;
1885
1886
    switch (PyTuple_Size(args)) {
1887
      case 0:
1888
        self->object = new Formattable();
1889
        self->flags = T_OWNED;
1890
        break;
1891
      case 1:
1892
        self->object = toFormattable(PyTuple_GET_ITEM(args, 0));
1893
        if (self->object)
1894
        {
1895
            self->flags = T_OWNED;
1896
            break;
1897
        }
1898
        PyErr_SetArgsError((PyObject *) self, "__init__", args);
1899
        return -1;
1900
      case 2:
1901
        if (!parseArgs(args, "Di", &date, &flag))
1902
        {
1903
            self->object = new Formattable(date, flag);
1904
            self->flags = T_OWNED;
1905
            break;
1906
        }
1907
        PyErr_SetArgsError((PyObject *) self, "__init__", args);
1908
        return -1;
1909
      default:
1910
        PyErr_SetArgsError((PyObject *) self, "__init__", args);
1911
        return -1;
1912
    }
1913
1914
    if (self->object)
1915
        return 0;
1916
1917
    return -1;
1918
}
1919
1920
static PyObject *t_formattable_getType(t_formattable *self)
1921
{
1922
    return PyInt_FromLong(self->object->getType());
1923
}
1924
1925
static PyObject *t_formattable_isNumeric(t_formattable *self)
1926
{
1927
    int b = self->object->isNumeric();
1928
    Py_RETURN_BOOL(b);
1929
}
1930
1931
static PyObject *t_formattable_getDouble(t_formattable *self)
1932
{
1933
    UErrorCode status = U_ZERO_ERROR;
1934
    double d = self->object->getDouble(status);
1935
1936
    if (U_FAILURE(status))
1937
        return ICUException(status).reportError();
1938
1939
    return PyFloat_FromDouble(d);
1940
}
1941
1942
static PyObject *t_formattable_getLong(t_formattable *self)
1943
{
1944
    UErrorCode status = U_ZERO_ERROR;
1945
    int n = self->object->getLong(status);
1946
1947
    if (U_FAILURE(status))
1948
        return ICUException(status).reportError();
1949
1950
    return PyInt_FromLong(n);
1951
}
1952
1953
static PyObject *t_formattable_getInt64(t_formattable *self)
1954
{
1955
    UErrorCode status = U_ZERO_ERROR;
1956
    PY_LONG_LONG l = self->object->getInt64(status);
1957
1958
    if (U_FAILURE(status))
1959
        return ICUException(status).reportError();
1960
1961
    return PyLong_FromLongLong(l);
1962
}
1963
1964
static PyObject *t_formattable_getDate(t_formattable *self)
1965
{
1966
    UErrorCode status = U_ZERO_ERROR;
1967
    double date = self->object->getDate(status);
1968
1969
    if (U_FAILURE(status))
1970
        return ICUException(status).reportError();
1971
1972
    return PyFloat_FromDouble(date / 1000.0);
1973
}
1974
1975
static PyObject *t_formattable_getString(t_formattable *self, PyObject *args)
1976
{
1977
    UErrorCode status = U_ZERO_ERROR;
1978
1979
    switch (PyTuple_Size(args)) {
1980
      case 0:
1981
      {
1982
          UnicodeString u;
1983
1984
          self->object->getString(u, status);
1985
          if (U_FAILURE(status))
1986
              return ICUException(status).reportError();
1987
1988
          return PyUnicode_FromUnicodeString(&u);
1989
      }
1990
      case 1:
1991
      {
1992
          PyObject *arg = PyTuple_GET_ITEM(args, 0);
1993
          UnicodeString *u;
1994
1995
          if (!parseArg(arg, "U", &u))
1996
          {
1997
              self->object->getString(*u, status);
1998
              if (U_FAILURE(status))
1999
                  return ICUException(status).reportError();
2000
2001
              Py_INCREF(arg);
2002
              return arg;
2003
          }
2004
          break;
2005
      }
2006
    }
2007
2008
    return PyErr_SetArgsError((PyObject *) self, "getString", args);
2009
}
2010
2011
static PyObject *t_formattable_setDouble(t_formattable *self, PyObject *arg)
2012
{
2013
    double d;
2014
    
2015
    if (!parseArg(arg, "d", &d))
2016
    {
2017
        self->object->setDouble(d);
2018
        Py_RETURN_NONE;
2019
    }
2020
2021
    return PyErr_SetArgsError((PyObject *) self, "setDouble", arg);
2022
}
2023
2024
static PyObject *t_formattable_setLong(t_formattable *self, PyObject *arg)
2025
{
2026
    int n;
2027
    
2028
    if (!parseArg(arg, "i", &n))
2029
    {
2030
        self->object->setLong(n);
2031
        Py_RETURN_NONE;
2032
    }
2033
2034
    return PyErr_SetArgsError((PyObject *) self, "setLong", arg);
2035
}
2036
2037
static PyObject *t_formattable_setInt64(t_formattable *self, PyObject *arg)
2038
{
2039
    PY_LONG_LONG l;
2040
    
2041
    if (!parseArg(arg, "L", &l))
2042
    {
2043
        self->object->setInt64(l);
2044
        Py_RETURN_NONE;
2045
    }
2046
2047
    return PyErr_SetArgsError((PyObject *) self, "setInt64", arg);
2048
}
2049
2050
static PyObject *t_formattable_setDate(t_formattable *self, PyObject *arg)
2051
{
2052
    double date;
2053
    
2054
    if (!parseArg(arg, "D", &date))
2055
    {
2056
        self->object->setDate(date);
2057
        Py_RETURN_NONE;
2058
    }
2059
2060
    return PyErr_SetArgsError((PyObject *) self, "setDate", arg);
2061
}
2062
2063
static PyObject *t_formattable_setString(t_formattable *self, PyObject *arg)
2064
{
2065
    UnicodeString *u;
2066
    UnicodeString _u;
2067
2068
    if (!parseArg(arg, "S", &u, &_u))
2069
    {
2070
        self->object->setString(*u);
2071
        Py_RETURN_NONE;
2072
    }
2073
2074
    return PyErr_SetArgsError((PyObject *) self, "setString", arg);
2075
}
2076
2077
static PyObject *t_formattable_richcmp(t_formattable *self,
2078
                                       PyObject *arg, int op)
2079
{
2080
    Formattable *f;
2081
2082
    if (!parseArg(arg, "P", TYPE_CLASSID(Formattable), &f))
2083
    {
2084
        int b = 0;
2085
2086
        switch (op) {
2087
          case Py_EQ:
2088
            b = *self->object == *f;
2089
            break;
2090
          case Py_NE:
2091
            b = *self->object != *f;
2092
            break;
2093
          default:
2094
            PyErr_SetNone(PyExc_NotImplementedError);
2095
            return NULL;
2096
        }
2097
2098
        Py_RETURN_BOOL(b);
2099
    }
2100
2101
    return PyErr_SetArgsError((PyObject *) self, "__richcmp__", arg);
2102
}
2103
2104
static PyObject *t_formattable_str(t_formattable *self)
2105
{
2106
    UErrorCode status = U_ZERO_ERROR;
2107
    UnicodeString u;
2108
2109
    switch (self->object->getType()) {
2110
      case Formattable::kDate:
2111
      {
2112
          SimpleDateFormat f = SimpleDateFormat(status);
2113
          if (U_FAILURE(status))
2114
              return ICUException(status).reportError();
2115
2116
          f.format(*self->object, u, status);
2117
          if (U_FAILURE(status))
2118
              return ICUException(status).reportError();
2119
          break;
2120
      }
2121
      case Formattable::kDouble:
2122
      case Formattable::kLong:
2123
      case Formattable::kInt64:
2124
      {
2125
          DecimalFormat f = DecimalFormat(status);
2126
          if (U_FAILURE(status))
2127
              return ICUException(status).reportError();
2128
2129
          f.format(*self->object, u, status);
2130
          if (U_FAILURE(status))
2131
              return ICUException(status).reportError();
2132
          break;
2133
      }
2134
      case Formattable::kString:
2135
      {
2136
          self->object->getString(u, status);
2137
          if (U_FAILURE(status))
2138
              return ICUException(status).reportError();
2139
          break;
2140
      }
2141
      default:
2142
        return t_uobject_str((t_uobject *) self);
2143
    }
2144
2145
    return PyUnicode_FromUnicodeString(&u);
2146
}
2147
2148
static PyObject *t_formattable_repr(t_formattable *self)
2149
{
2150
    PyObject *name = PyObject_GetAttrString((PyObject *) self->ob_type,
2151
                                            "__name__");
2152
    PyObject *str = self->ob_type->tp_str((PyObject *) self);
2153
2154
    if (str)
2155
    {
2156
        PyObject *repr = str->ob_type->tp_repr(str);
2157
        Py_DECREF(str);
2158
        str = repr;
2159
    }
2160
    if (!str)
2161
        return NULL;
2162
    
2163
#if PY_VERSION_HEX < 0x02040000
2164
    PyObject *args = Py_BuildValue("(OO)", name, str);
2165
#else
2166
    PyObject *args = PyTuple_Pack(2, name, str);
2167
#endif
2168
    PyObject *format = PyString_FromString("<%s: %s>");
2169
    PyObject *repr = PyString_Format(format, args);
2170
2171
    Py_DECREF(name);
2172
    Py_DECREF(str);
2173
    Py_DECREF(args);
2174
    Py_DECREF(format);
2175
2176
    return repr;
2177
}
2178
2179
2180
/* MeasureUnit */
2181
2182
static PyObject *t_measureunit_richcmp(t_measureunit *self,
2183
                                       PyObject *arg, int op)
2184
{
2185
    int b = 0;
2186
2187
    switch (op) {
2188
      case Py_EQ:
2189
      case Py_NE:
2190
        if (PyObject_TypeCheck(arg, &UObjectType))
2191
            b = *self->object == *((t_uobject *) arg)->object;
2192
        if (op == Py_EQ)
2193
            Py_RETURN_BOOL(b);
2194
        Py_RETURN_BOOL(!b);
2195
      case Py_LT:
2196
      case Py_LE:
2197
      case Py_GT:
2198
      case Py_GE:
2199
        PyErr_SetNone(PyExc_NotImplementedError);
2200
        return NULL;
2201
    }
2202
2203
    return NULL;
2204
}
2205
2206
2207
/* Measure */
2208
2209
static PyObject *t_measure_getNumber(t_measure *self)
2210
{
2211
    Formattable *f = new Formattable(self->object->getNumber());
2212
    return wrap_Formattable(f, T_OWNED);
2213
}
2214
2215
static PyObject *t_measure_getUnit(t_measure *self)
2216
{
2217
    MeasureUnit *u = (MeasureUnit *) self->object->getUnit().clone();
2218
    return wrap_MeasureUnit(u, T_OWNED);
2219
}
2220
2221
static PyObject *t_measure_richcmp(t_measure *self, PyObject *arg, int op)
2222
{
2223
    int b = 0;
2224
2225
    switch (op) {
2226
      case Py_EQ:
2227
      case Py_NE:
2228
        if (PyObject_TypeCheck(arg, &UObjectType))
2229
            b = *self->object == *((t_uobject *) arg)->object;
2230
        if (op == Py_EQ)
2231
            Py_RETURN_BOOL(b);
2232
        Py_RETURN_BOOL(!b);
2233
      case Py_LT:
2234
      case Py_LE:
2235
      case Py_GT:
2236
      case Py_GE:
2237
        PyErr_SetNone(PyExc_NotImplementedError);
2238
        return NULL;
2239
    }
2240
2241
    return NULL;
2242
}
2243
2244
2245
/* CurrencyUnit */
2246
2247
static int t_currencyunit_init(t_currencyunit *self,
2248
                               PyObject *args, PyObject *kwds)
2249
{
2250
    UErrorCode status = U_ZERO_ERROR;
2251
    UnicodeString *u;
2252
    UnicodeString _u;
2253
2254
    if (!parseArgs(args, "S", &u, &_u))
2255
    {
2256
        CurrencyUnit *cu = new CurrencyUnit(u->getTerminatedBuffer(), status);
2257
2258
        if (U_FAILURE(status))
2259
        {
2260
            ICUException(status).reportError();
2261
            return -1;
2262
        }
2263
2264
        self->object = cu;
2265
        self->flags = T_OWNED;
2266
2267
        return 0;
2268
    }
2269
2270
    PyErr_SetArgsError((PyObject *) self, "__init__", args);
2271
    return -1;
2272
}
2273
2274
static PyObject *t_currencyunit_getISOCurrency(t_currencyunit *self)
2275
{
2276
    UnicodeString u(self->object->getISOCurrency());
2277
    return PyUnicode_FromUnicodeString(&u);
2278
}
2279
2280
static PyObject *t_currencyunit_str(t_currencyunit *self)
2281
{
2282
    UnicodeString u(self->object->getISOCurrency());
2283
    return PyUnicode_FromUnicodeString(&u);
2284
}
2285
2286
2287
/* CurrencyAmount */
2288
2289
static int t_currencyamount_init(t_currencyamount *self,
2290
                                 PyObject *args, PyObject *kwds)
2291
{
2292
    UErrorCode status = U_ZERO_ERROR;
2293
    Formattable *f;
2294
    double d;
2295
    UnicodeString *u;
2296
    UnicodeString _u;
2297
2298
    if (!parseArgs(args, "PS", TYPE_CLASSID(Formattable),
2299
                   &f, &u, &_u))
2300
    {
2301
        CurrencyAmount *ca =
2302
            new CurrencyAmount(*f, u->getTerminatedBuffer(), status);
2303
2304
        if (U_FAILURE(status))
2305
        {
2306
            ICUException(status).reportError();
2307
            return -1;
2308
        }
2309
2310
        self->object = ca;
2311
        self->flags = T_OWNED;
2312
2313
        return 0;
2314
    }
2315
2316
    if (!parseArgs(args, "dS", &d, &u, &_u))
2317
    {
2318
        CurrencyAmount *ca =
2319
            new CurrencyAmount(d, u->getTerminatedBuffer(), status);
2320
2321
        if (U_FAILURE(status))
2322
        {
2323
            ICUException(status).reportError();
2324
            return -1;
2325
        }
2326
2327
        self->object = ca;
2328
        self->flags = T_OWNED;
2329
2330
        return 0;
2331
    }
2332
2333
    PyErr_SetArgsError((PyObject *) self, "__init__", args);
2334
    return -1;
2335
}
2336
2337
static PyObject *t_currencyamount_getCurrency(t_currencyamount *self)
2338
{
2339
    CurrencyUnit *cu = new CurrencyUnit(self->object->getCurrency());
2340
    return wrap_CurrencyUnit(cu, T_OWNED);
2341
}
2342
2343
static PyObject *t_currencyamount_str(t_currencyamount *self)
2344
{
2345
    UnicodeString u(self->object->getISOCurrency());
2346
    PyObject *currency = PyUnicode_FromUnicodeString(&u);
2347
2348
    Formattable number = self->object->getNumber();
2349
    PyObject *amount = PyFloat_FromDouble(number.getDouble());
2350
2351
    PyObject *format = PyString_FromString("%s %0.2f");
2352
    PyObject *tuple = PyTuple_New(2);
2353
    PyObject *str;
2354
2355
    PyTuple_SET_ITEM(tuple, 0, currency);
2356
    PyTuple_SET_ITEM(tuple, 1, amount);
2357
    str = PyString_Format(format, tuple);
2358
    Py_DECREF(format);
2359
    Py_DECREF(tuple);
2360
2361
    return str;
2362
}
2363
2364
2365
/* StringEnumeration */
2366
2367
static PyObject *t_stringenumeration_count(t_stringenumeration *self)
2368
{
2369
    UErrorCode status = U_ZERO_ERROR;
2370
    int i = self->object->count(status);
2371
  
2372
    if (U_FAILURE(status))
2373
        return ICUException(status).reportError();
2374
2375
    return PyInt_FromLong(i);
2376
}
2377
2378
static PyObject *t_stringenumeration_reset(t_stringenumeration *self)
2379
{
2380
    UErrorCode status = U_ZERO_ERROR;
2381
  
2382
    self->object->reset(status);
2383
    if (U_FAILURE(status))
2384
        return ICUException(status).reportError();
2385
2386
    Py_RETURN_NONE;
2387
}
2388
2389
static PyObject *t_stringenumeration_next(t_stringenumeration *self)
2390
{
2391
    int32_t len;
2392
    UErrorCode status = U_ZERO_ERROR;
2393
    const char *str = self->object->next(&len, status);
2394
2395
    if (U_FAILURE(status))
2396
        return ICUException(status).reportError();
2397
2398
    if (str == NULL)
2399
    {
2400
        PyErr_SetNone(PyExc_StopIteration);
2401
        return NULL;
2402
    }
2403
2404
    return PyString_FromStringAndSize(str, len);
2405
}
2406
2407
static PyObject *t_stringenumeration_unext(t_stringenumeration *self)
2408
{
2409
    int32_t len;
2410
    UErrorCode status = U_ZERO_ERROR;
2411
    const UChar *str = self->object->unext(&len, status);
2412
2413
    if (U_FAILURE(status))
2414
        return ICUException(status).reportError();
2415
2416
    if (str == NULL)
2417
    {
2418
        PyErr_SetNone(PyExc_StopIteration);
2419
        return NULL;
2420
    }
2421
2422
    UnicodeString u(str);
2423
    return PyUnicode_FromUnicodeString(&u);
2424
}
2425
2426
static PyObject *t_stringenumeration_snext(t_stringenumeration *self)
2427
{
2428
    UErrorCode status = U_ZERO_ERROR;
2429
    const UnicodeString *str = self->object->snext(status);
2430
2431
    if (U_FAILURE(status))
2432
        return ICUException(status).reportError();
2433
2434
    if (str == NULL)
2435
    {
2436
        PyErr_SetNone(PyExc_StopIteration);
2437
        return NULL;
2438
    }
2439
2440
    return wrap_UnicodeString(new UnicodeString(*str), T_OWNED);
2441
}                
2442
2443
static PyObject *t_stringenumeration_iter(t_stringenumeration *self)
2444
{
2445
    Py_INCREF(self);
2446
    return (PyObject *) self;
2447
}
2448
2449
2450
void _init_bases(PyObject *m)
2451
{
2452
    UnicodeStringType.tp_str = (reprfunc) t_unicodestring_str;
2453
    UnicodeStringType.tp_repr = (reprfunc) t_unicodestring_repr;
2454
    UnicodeStringType.tp_richcompare = (richcmpfunc) t_unicodestring_richcmp;
2455
    UnicodeStringType.tp_as_sequence = &t_unicodestring_as_sequence;
2456
    FormattableType.tp_richcompare = (richcmpfunc) t_formattable_richcmp;
2457
    FormattableType.tp_str = (reprfunc) t_formattable_str;
2458
    FormattableType.tp_repr = (reprfunc) t_formattable_repr;
2459
    MeasureUnitType.tp_richcompare = (richcmpfunc) t_measureunit_richcmp;
2460
    MeasureType.tp_richcompare = (richcmpfunc) t_measure_richcmp;
2461
    CurrencyUnitType.tp_str = (reprfunc) t_currencyunit_str;
2462
    CurrencyAmountType.tp_str = (reprfunc) t_currencyamount_str;
2463
    StringEnumerationType.tp_iter = (getiterfunc) t_stringenumeration_iter;
2464
    StringEnumerationType.tp_iternext = (iternextfunc) t_stringenumeration_next;
2465
2466
    INSTALL_TYPE(UObject, m);
2467
    INSTALL_TYPE(Replaceable, m);
2468
    REGISTER_TYPE(UnicodeString, m);
2469
    REGISTER_TYPE(Formattable, m);
2470
    INSTALL_TYPE(MeasureUnit, m);
2471
    INSTALL_TYPE(Measure, m);
2472
    REGISTER_TYPE(CurrencyUnit, m);
2473
    REGISTER_TYPE(CurrencyAmount, m);
2474
    INSTALL_TYPE(StringEnumeration, m);
2475
2476
    INSTALL_MODULE_INT(m, U_FOLD_CASE_DEFAULT);
2477
    INSTALL_MODULE_INT(m, U_COMPARE_CODE_POINT_ORDER);
2478
    INSTALL_MODULE_INT(m, U_FOLD_CASE_EXCLUDE_SPECIAL_I);
2479
2480
    INSTALL_MODULE_INT(m, UIDNA_DEFAULT);
2481
    INSTALL_MODULE_INT(m, UIDNA_ALLOW_UNASSIGNED);
2482
    INSTALL_MODULE_INT(m, UIDNA_USE_STD3_RULES);
2483
2484
    INSTALL_STATIC_INT(Formattable, kIsDate);
2485
    INSTALL_STATIC_INT(Formattable, kDate);
2486
    INSTALL_STATIC_INT(Formattable, kDouble);
2487
    INSTALL_STATIC_INT(Formattable, kLong);
2488
    INSTALL_STATIC_INT(Formattable, kString);
2489
    INSTALL_STATIC_INT(Formattable, kArray);
2490
    INSTALL_STATIC_INT(Formattable, kInt64);
2491
    INSTALL_STATIC_INT(Formattable, kObject);
2492
}