~ubuntu-branches/ubuntu/natty/python3.1/natty-security

« back to all changes in this revision

Viewing changes to Objects/bytes_methods.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2010-07-06 16:52:42 UTC
  • mfrom: (1.2.1 upstream) (2.1.11 sid)
  • Revision ID: james.westby@ubuntu.com-20100706165242-2xv4i019r3et6c0j
Tags: 3.1.2+20100706-1ubuntu1
* Merge with Debian; remaining changes:
  - Regenerate the control file.
  - Add debian/patches/overwrite-semaphore-check for Lucid buildds.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 
25
25
    e = p + len;
26
26
    for (; p < e; p++) {
27
 
        if (!Py_ISSPACE(*p))
 
27
        if (!Py_ISSPACE(*p))
28
28
            Py_RETURN_FALSE;
29
29
    }
30
30
    Py_RETURN_TRUE;
46
46
 
47
47
    /* Shortcut for single character strings */
48
48
    if (len == 1 && Py_ISALPHA(*p))
49
 
        Py_RETURN_TRUE;
 
49
        Py_RETURN_TRUE;
50
50
 
51
51
    /* Special case for empty strings */
52
52
    if (len == 0)
53
 
        Py_RETURN_FALSE;
 
53
        Py_RETURN_FALSE;
54
54
 
55
55
    e = p + len;
56
56
    for (; p < e; p++) {
57
 
        if (!Py_ISALPHA(*p))
58
 
            Py_RETURN_FALSE;
 
57
        if (!Py_ISALPHA(*p))
 
58
            Py_RETURN_FALSE;
59
59
    }
60
60
    Py_RETURN_TRUE;
61
61
}
76
76
 
77
77
    /* Shortcut for single character strings */
78
78
    if (len == 1 && Py_ISALNUM(*p))
79
 
        Py_RETURN_TRUE;
 
79
        Py_RETURN_TRUE;
80
80
 
81
81
    /* Special case for empty strings */
82
82
    if (len == 0)
83
 
        Py_RETURN_FALSE;
 
83
        Py_RETURN_FALSE;
84
84
 
85
85
    e = p + len;
86
86
    for (; p < e; p++) {
87
 
        if (!Py_ISALNUM(*p))
88
 
            Py_RETURN_FALSE;
 
87
        if (!Py_ISALNUM(*p))
 
88
            Py_RETURN_FALSE;
89
89
    }
90
90
    Py_RETURN_TRUE;
91
91
}
106
106
 
107
107
    /* Shortcut for single character strings */
108
108
    if (len == 1 && Py_ISDIGIT(*p))
109
 
        Py_RETURN_TRUE;
 
109
        Py_RETURN_TRUE;
110
110
 
111
111
    /* Special case for empty strings */
112
112
    if (len == 0)
113
 
        Py_RETURN_FALSE;
 
113
        Py_RETURN_FALSE;
114
114
 
115
115
    e = p + len;
116
116
    for (; p < e; p++) {
117
 
        if (!Py_ISDIGIT(*p))
118
 
            Py_RETURN_FALSE;
 
117
        if (!Py_ISDIGIT(*p))
 
118
            Py_RETURN_FALSE;
119
119
    }
120
120
    Py_RETURN_TRUE;
121
121
}
137
137
 
138
138
    /* Shortcut for single character strings */
139
139
    if (len == 1)
140
 
        return PyBool_FromLong(Py_ISLOWER(*p));
 
140
        return PyBool_FromLong(Py_ISLOWER(*p));
141
141
 
142
142
    /* Special case for empty strings */
143
143
    if (len == 0)
144
 
        Py_RETURN_FALSE;
 
144
        Py_RETURN_FALSE;
145
145
 
146
146
    e = p + len;
147
147
    cased = 0;
148
148
    for (; p < e; p++) {
149
 
        if (Py_ISUPPER(*p))
150
 
            Py_RETURN_FALSE;
151
 
        else if (!cased && Py_ISLOWER(*p))
152
 
            cased = 1;
 
149
        if (Py_ISUPPER(*p))
 
150
            Py_RETURN_FALSE;
 
151
        else if (!cased && Py_ISLOWER(*p))
 
152
            cased = 1;
153
153
    }
154
154
    return PyBool_FromLong(cased);
155
155
}
171
171
 
172
172
    /* Shortcut for single character strings */
173
173
    if (len == 1)
174
 
        return PyBool_FromLong(Py_ISUPPER(*p));
 
174
        return PyBool_FromLong(Py_ISUPPER(*p));
175
175
 
176
176
    /* Special case for empty strings */
177
177
    if (len == 0)
178
 
        Py_RETURN_FALSE;
 
178
        Py_RETURN_FALSE;
179
179
 
180
180
    e = p + len;
181
181
    cased = 0;
182
182
    for (; p < e; p++) {
183
 
        if (Py_ISLOWER(*p))
184
 
            Py_RETURN_FALSE;
185
 
        else if (!cased && Py_ISUPPER(*p))
186
 
            cased = 1;
 
183
        if (Py_ISLOWER(*p))
 
184
            Py_RETURN_FALSE;
 
185
        else if (!cased && Py_ISUPPER(*p))
 
186
            cased = 1;
187
187
    }
188
188
    return PyBool_FromLong(cased);
189
189
}
207
207
 
208
208
    /* Shortcut for single character strings */
209
209
    if (len == 1)
210
 
        return PyBool_FromLong(Py_ISUPPER(*p));
 
210
        return PyBool_FromLong(Py_ISUPPER(*p));
211
211
 
212
212
    /* Special case for empty strings */
213
213
    if (len == 0)
214
 
        Py_RETURN_FALSE;
 
214
        Py_RETURN_FALSE;
215
215
 
216
216
    e = p + len;
217
217
    cased = 0;
218
218
    previous_is_cased = 0;
219
219
    for (; p < e; p++) {
220
 
        register const unsigned char ch = *p;
 
220
        register const unsigned char ch = *p;
221
221
 
222
 
        if (Py_ISUPPER(ch)) {
223
 
            if (previous_is_cased)
224
 
                Py_RETURN_FALSE;
225
 
            previous_is_cased = 1;
226
 
            cased = 1;
227
 
        }
228
 
        else if (Py_ISLOWER(ch)) {
229
 
            if (!previous_is_cased)
230
 
                Py_RETURN_FALSE;
231
 
            previous_is_cased = 1;
232
 
            cased = 1;
233
 
        }
234
 
        else
235
 
            previous_is_cased = 0;
 
222
        if (Py_ISUPPER(ch)) {
 
223
            if (previous_is_cased)
 
224
                Py_RETURN_FALSE;
 
225
            previous_is_cased = 1;
 
226
            cased = 1;
 
227
        }
 
228
        else if (Py_ISLOWER(ch)) {
 
229
            if (!previous_is_cased)
 
230
                Py_RETURN_FALSE;
 
231
            previous_is_cased = 1;
 
232
            cased = 1;
 
233
        }
 
234
        else
 
235
            previous_is_cased = 0;
236
236
    }
237
237
    return PyBool_FromLong(cased);
238
238
}
246
246
void
247
247
_Py_bytes_lower(char *result, const char *cptr, Py_ssize_t len)
248
248
{
249
 
        Py_ssize_t i;
 
249
        Py_ssize_t i;
250
250
 
251
251
        /*
252
 
        newobj = PyBytes_FromStringAndSize(NULL, len);
253
 
        if (!newobj)
254
 
                return NULL;
 
252
        newobj = PyBytes_FromStringAndSize(NULL, len);
 
253
        if (!newobj)
 
254
                return NULL;
255
255
 
256
 
        s = PyBytes_AS_STRING(newobj);
 
256
        s = PyBytes_AS_STRING(newobj);
257
257
        */
258
258
 
259
 
        Py_MEMCPY(result, cptr, len);
 
259
        Py_MEMCPY(result, cptr, len);
260
260
 
261
 
        for (i = 0; i < len; i++) {
262
 
                int c = Py_CHARMASK(result[i]);
263
 
                if (Py_ISUPPER(c))
264
 
                        result[i] = Py_TOLOWER(c);
265
 
        }
 
261
        for (i = 0; i < len; i++) {
 
262
                int c = Py_CHARMASK(result[i]);
 
263
                if (Py_ISUPPER(c))
 
264
                        result[i] = Py_TOLOWER(c);
 
265
        }
266
266
}
267
267
 
268
268
 
274
274
void
275
275
_Py_bytes_upper(char *result, const char *cptr, Py_ssize_t len)
276
276
{
277
 
        Py_ssize_t i;
 
277
        Py_ssize_t i;
278
278
 
279
279
        /*
280
 
        newobj = PyBytes_FromStringAndSize(NULL, len);
281
 
        if (!newobj)
282
 
                return NULL;
 
280
        newobj = PyBytes_FromStringAndSize(NULL, len);
 
281
        if (!newobj)
 
282
                return NULL;
283
283
 
284
 
        s = PyBytes_AS_STRING(newobj);
 
284
        s = PyBytes_AS_STRING(newobj);
285
285
        */
286
286
 
287
 
        Py_MEMCPY(result, cptr, len);
 
287
        Py_MEMCPY(result, cptr, len);
288
288
 
289
 
        for (i = 0; i < len; i++) {
290
 
                int c = Py_CHARMASK(result[i]);
291
 
                if (Py_ISLOWER(c))
292
 
                        result[i] = Py_TOUPPER(c);
293
 
        }
 
289
        for (i = 0; i < len; i++) {
 
290
                int c = Py_CHARMASK(result[i]);
 
291
                if (Py_ISLOWER(c))
 
292
                        result[i] = Py_TOUPPER(c);
 
293
        }
294
294
}
295
295
 
296
296
 
303
303
void
304
304
_Py_bytes_title(char *result, char *s, Py_ssize_t len)
305
305
{
306
 
        Py_ssize_t i;
307
 
        int previous_is_cased = 0;
 
306
        Py_ssize_t i;
 
307
        int previous_is_cased = 0;
308
308
 
309
309
        /*
310
 
        newobj = PyBytes_FromStringAndSize(NULL, len);
311
 
        if (newobj == NULL)
312
 
                return NULL;
313
 
        s_new = PyBytes_AsString(newobj);
 
310
        newobj = PyBytes_FromStringAndSize(NULL, len);
 
311
        if (newobj == NULL)
 
312
                return NULL;
 
313
        s_new = PyBytes_AsString(newobj);
314
314
        */
315
 
        for (i = 0; i < len; i++) {
316
 
                int c = Py_CHARMASK(*s++);
317
 
                if (Py_ISLOWER(c)) {
318
 
                        if (!previous_is_cased)
319
 
                            c = Py_TOUPPER(c);
320
 
                        previous_is_cased = 1;
321
 
                } else if (Py_ISUPPER(c)) {
322
 
                        if (previous_is_cased)
323
 
                            c = Py_TOLOWER(c);
324
 
                        previous_is_cased = 1;
325
 
                } else
326
 
                        previous_is_cased = 0;
327
 
                *result++ = c;
328
 
        }
 
315
        for (i = 0; i < len; i++) {
 
316
                int c = Py_CHARMASK(*s++);
 
317
                if (Py_ISLOWER(c)) {
 
318
                        if (!previous_is_cased)
 
319
                            c = Py_TOUPPER(c);
 
320
                        previous_is_cased = 1;
 
321
                } else if (Py_ISUPPER(c)) {
 
322
                        if (previous_is_cased)
 
323
                            c = Py_TOLOWER(c);
 
324
                        previous_is_cased = 1;
 
325
                } else
 
326
                        previous_is_cased = 0;
 
327
                *result++ = c;
 
328
        }
329
329
}
330
330
 
331
331
 
332
332
PyDoc_STRVAR_shared(_Py_capitalize__doc__,
333
333
"B.capitalize() -> copy of B\n\
334
334
\n\
335
 
Return a copy of B with only its first character capitalized (ASCII).");
 
335
Return a copy of B with only its first character capitalized (ASCII)\n\
 
336
and the rest lower-cased.");
336
337
 
337
338
void
338
339
_Py_bytes_capitalize(char *result, char *s, Py_ssize_t len)
339
340
{
340
 
        Py_ssize_t i;
 
341
        Py_ssize_t i;
341
342
 
342
343
        /*
343
 
        newobj = PyBytes_FromStringAndSize(NULL, len);
344
 
        if (newobj == NULL)
345
 
                return NULL;
346
 
        s_new = PyBytes_AsString(newobj);
 
344
        newobj = PyBytes_FromStringAndSize(NULL, len);
 
345
        if (newobj == NULL)
 
346
                return NULL;
 
347
        s_new = PyBytes_AsString(newobj);
347
348
        */
348
 
        if (0 < len) {
349
 
                int c = Py_CHARMASK(*s++);
350
 
                if (Py_ISLOWER(c))
351
 
                        *result = Py_TOUPPER(c);
352
 
                else
353
 
                        *result = c;
354
 
                result++;
355
 
        }
356
 
        for (i = 1; i < len; i++) {
357
 
                int c = Py_CHARMASK(*s++);
358
 
                if (Py_ISUPPER(c))
359
 
                        *result = Py_TOLOWER(c);
360
 
                else
361
 
                        *result = c;
362
 
                result++;
363
 
        }
 
349
        if (0 < len) {
 
350
                int c = Py_CHARMASK(*s++);
 
351
                if (Py_ISLOWER(c))
 
352
                        *result = Py_TOUPPER(c);
 
353
                else
 
354
                        *result = c;
 
355
                result++;
 
356
        }
 
357
        for (i = 1; i < len; i++) {
 
358
                int c = Py_CHARMASK(*s++);
 
359
                if (Py_ISUPPER(c))
 
360
                        *result = Py_TOLOWER(c);
 
361
                else
 
362
                        *result = c;
 
363
                result++;
 
364
        }
364
365
}
365
366
 
366
367
 
373
374
void
374
375
_Py_bytes_swapcase(char *result, char *s, Py_ssize_t len)
375
376
{
376
 
        Py_ssize_t i;
 
377
        Py_ssize_t i;
377
378
 
378
379
        /*
379
 
        newobj = PyBytes_FromStringAndSize(NULL, len);
380
 
        if (newobj == NULL)
381
 
                return NULL;
382
 
        s_new = PyBytes_AsString(newobj);
 
380
        newobj = PyBytes_FromStringAndSize(NULL, len);
 
381
        if (newobj == NULL)
 
382
                return NULL;
 
383
        s_new = PyBytes_AsString(newobj);
383
384
        */
384
 
        for (i = 0; i < len; i++) {
385
 
                int c = Py_CHARMASK(*s++);
386
 
                if (Py_ISLOWER(c)) {
387
 
                        *result = Py_TOUPPER(c);
388
 
                }
389
 
                else if (Py_ISUPPER(c)) {
390
 
                        *result = Py_TOLOWER(c);
391
 
                }
392
 
                else
393
 
                        *result = c;
394
 
                result++;
395
 
        }
 
385
        for (i = 0; i < len; i++) {
 
386
                int c = Py_CHARMASK(*s++);
 
387
                if (Py_ISLOWER(c)) {
 
388
                        *result = Py_TOUPPER(c);
 
389
                }
 
390
                else if (Py_ISUPPER(c)) {
 
391
                        *result = Py_TOLOWER(c);
 
392
                }
 
393
                else
 
394
                        *result = c;
 
395
                result++;
 
396
        }
396
397
}
397
398
 
398
399
 
425
426
PyObject *
426
427
_Py_bytes_maketrans(PyObject *args)
427
428
{
428
 
        PyObject *frm, *to, *res = NULL;
429
 
        Py_buffer bfrm, bto;
430
 
        Py_ssize_t i;
431
 
        char *p;
432
 
 
433
 
        bfrm.len = -1;
434
 
        bto.len = -1;
435
 
        
436
 
        if (!PyArg_ParseTuple(args, "OO:maketrans", &frm, &to))
437
 
                return NULL;
438
 
        if (_getbuffer(frm, &bfrm) < 0)
439
 
                return NULL;
440
 
        if (_getbuffer(to, &bto) < 0)
441
 
                goto done;
442
 
        if (bfrm.len != bto.len) {
443
 
                PyErr_Format(PyExc_ValueError,
444
 
                             "maketrans arguments must have same length");
445
 
                goto done;
446
 
        }
447
 
        res = PyBytes_FromStringAndSize(NULL, 256);
448
 
        if (!res) {
449
 
                goto done;
450
 
        }
451
 
        p = PyBytes_AS_STRING(res);
452
 
        for (i = 0; i < 256; i++)
453
 
                p[i] = i;
454
 
        for (i = 0; i < bfrm.len; i++) {
455
 
                p[((unsigned char *)bfrm.buf)[i]] = ((char *)bto.buf)[i];
456
 
        }
 
429
        PyObject *frm, *to, *res = NULL;
 
430
        Py_buffer bfrm, bto;
 
431
        Py_ssize_t i;
 
432
        char *p;
 
433
 
 
434
        bfrm.len = -1;
 
435
        bto.len = -1;
 
436
 
 
437
        if (!PyArg_ParseTuple(args, "OO:maketrans", &frm, &to))
 
438
                return NULL;
 
439
        if (_getbuffer(frm, &bfrm) < 0)
 
440
                return NULL;
 
441
        if (_getbuffer(to, &bto) < 0)
 
442
                goto done;
 
443
        if (bfrm.len != bto.len) {
 
444
                PyErr_Format(PyExc_ValueError,
 
445
                             "maketrans arguments must have same length");
 
446
                goto done;
 
447
        }
 
448
        res = PyBytes_FromStringAndSize(NULL, 256);
 
449
        if (!res) {
 
450
                goto done;
 
451
        }
 
452
        p = PyBytes_AS_STRING(res);
 
453
        for (i = 0; i < 256; i++)
 
454
                p[i] = i;
 
455
        for (i = 0; i < bfrm.len; i++) {
 
456
                p[((unsigned char *)bfrm.buf)[i]] = ((char *)bto.buf)[i];
 
457
        }
457
458
 
458
459
  done:
459
 
        if (bfrm.len != -1)
460
 
                PyBuffer_Release(&bfrm);
461
 
        if (bto.len != -1)
462
 
                PyBuffer_Release(&bto);
463
 
        return res;
 
460
        if (bfrm.len != -1)
 
461
                PyBuffer_Release(&bfrm);
 
462
        if (bto.len != -1)
 
463
                PyBuffer_Release(&bto);
 
464
        return res;
464
465
}