Commit | Line | Data |
---|---|---|
5b1830f3 MW |
1 | ### -*-pyrex-*- |
2 | ### | |
3 | ### String utilities | |
4 | ### | |
5 | ### (c) 2006 Straylight/Edgeware | |
6 | ### | |
6a6bd8fa | 7 | |
5b1830f3 MW |
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. | |
6a6bd8fa MW |
25 | |
26 | def word(char *p, quotep = False): | |
addc0c37 | 27 | """word(STR, [quotep = False]) -> WORD, REST""" |
6a6bd8fa MW |
28 | cdef unsigned f |
29 | cdef char *op | |
30 | cdef char *pp | |
31 | cdef char *q | |
32 | cdef object w | |
33 | cdef object r | |
34 | ||
35 | f = 0 | |
36 | if quotep: | |
37 | f = f | STRF_QUOTE | |
38 | pp = op = xstrdup(p) | |
39 | q = str_qword(&pp, f) | |
40 | if q is NULL: | |
41 | w = None | |
42 | else: | |
43 | w = q | |
44 | if pp is NULL: | |
45 | r = '' | |
46 | else: | |
47 | r = pp | |
48 | xfree(op) | |
49 | return w, r | |
50 | ||
51 | def split(char *p, int n = -1, quotep = False): | |
addc0c37 | 52 | """split(STR, [n = -1], [quotep = False]) -> WORDS, REST""" |
6a6bd8fa MW |
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 | ||
95920c4f | 79 | def match(char *p, char *s, prefixp = False): |
addc0c37 | 80 | """match(PAT, STR, [prefixp = False]) -> BOOL""" |
95920c4f | 81 | cdef unsigned f |
23bff39b MW |
82 | |
83 | f = 0 | |
95920c4f MW |
84 | if prefixp: |
85 | f = f | STRF_PREFIX | |
86 | return _tobool(str_matchx(p, s, f)) | |
6a6bd8fa MW |
87 | |
88 | def sanitize(char *p, int n = -1): | |
addc0c37 | 89 | """sanitize(STR, [n = -1]) -> STR""" |
6a6bd8fa MW |
90 | cdef char *buf |
91 | cdef object d | |
92 | ||
93 | if n < 0: | |
94 | n = strlen(p) | |
95 | buf = <char *>xmalloc(n + 1) | |
96 | str_sanitize(buf, p, n + 1) | |
97 | d = buf | |
98 | xfree(buf) | |
99 | return d | |
ae68e889 MW |
100 | |
101 | def versioncmp(char *va, char *vb): | |
addc0c37 | 102 | """versioncmp(V0, V1) -> -1 | 0 | +1""" |
ae68e889 | 103 | return _versioncmp(va, vb) |
6a6bd8fa | 104 | |
5b1830f3 | 105 | ###----- That's all, folks -------------------------------------------------- |