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