chiark / gitweb /
debian: Just the one Python version now.
[mLib-python] / str.pyx
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
28 def 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
52 def 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
79 def match(char *p, char *s, prefixp = False):
80   cdef unsigned f
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
98 #----- That's all, folks ----------------------------------------------------