chiark / gitweb /
_u32: Allow LONG_MAX to be stored in a Python int.
[mLib-python] / str.pyx
... / ...
CommitLineData
1# -*-pyrex-*-
2#
3# $Id$
4#
5# String utilities
6#
7# (c) 2006 Straylight/Edgeware
8#
9
10#----- Licensing notice -----------------------------------------------------
11#
12# This file is part of the Python interface to mLib.
13#
14# mLib/Python is free software; you can redistribute it and/or modify
15# it under the terms of the GNU General Public License as published by
16# the Free Software Foundation; either version 2 of the License, or
17# (at your option) any later version.
18#
19# mLib/Python is distributed in the hope that it will be useful,
20# but WITHOUT ANY WARRANTY; without even the implied warranty of
21# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22# GNU General Public License for more details.
23#
24# You should have received a copy of the GNU General Public License
25# along with mLib/Python; if not, write to the Free Software Foundation,
26# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27
28def word(char *p, quotep = False):
29 cdef unsigned f
30 cdef char *op
31 cdef char *pp
32 cdef char *q
33 cdef object w
34 cdef object r
35
36 f = 0
37 if quotep:
38 f = f | STRF_QUOTE
39 pp = op = xstrdup(p)
40 q = str_qword(&pp, f)
41 if q is NULL:
42 w = None
43 else:
44 w = q
45 if pp is NULL:
46 r = ''
47 else:
48 r = pp
49 xfree(op)
50 return w, r
51
52def split(char *p, int n = -1, quotep = False):
53 cdef unsigned f
54 cdef char *op
55 cdef char *pp
56 cdef char *q
57 cdef object l
58 cdef object r
59
60 f = 0
61 if quotep:
62 f = f | STRF_QUOTE
63 l = []
64 op = pp = xstrdup(p)
65 while n != 0:
66 q = str_qword(&pp, f)
67 if q is NULL:
68 break
69 l.append(q)
70 if n > 0:
71 n = n - 1
72 if pp is NULL:
73 r = ''
74 else:
75 r = pp
76 xfree(op)
77 return l, r
78
79def match(char *p, char *s):
80 return _tobool(str_match(p, s))
81
82def sanitize(char *p, int n = -1):
83 cdef char *buf
84 cdef object d
85
86 if n < 0:
87 n = strlen(p)
88 buf = <char *>xmalloc(n + 1)
89 str_sanitize(buf, p, n + 1)
90 d = buf
91 xfree(buf)
92 return d
93
94
95#----- That's all, folks ----------------------------------------------------