LP-500 HoTT Telemetry added device definitions
[librepilot.git] / flight / libraries / PyMite / lib / string.py
blob493644b641f4cd4c0be0b7c580b6b9343feec02c
1 # This file is Copyright 2009, 2010 Dean Hall.
3 # This file is part of the Python-on-a-Chip program.
4 # Python-on-a-Chip is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1.
7 # Python-on-a-Chip is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 # A copy of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1
11 # is seen in the file COPYING in this directory.
13 ## @file
15 ## @package string
16 # @brief Provides PyMite's string module.
20 """__NATIVE__
21 #include <stdlib.h>
22 #include <string.h>
23 """
26 digits = "0123456789"
27 hexdigits = "0123456789abcdefABCDEF"
28 letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
32 # Returns the integer represented by the string a [in base b].
33 # Optional int arg, b, may be 0 or 2 through 36; otherwise it is a ValueError.
35 def atoi(a, b):
36 """__NATIVE__
37 pPmObj_t pa;
38 pPmObj_t pb;
39 char const *pc;
40 char *pend;
41 long i;
42 int8_t base;
43 pPmObj_t pi;
44 PmReturn_t retval = PM_RET_OK;
46 /* Raise TypeError if it's not a string or wrong number of args, */
47 pa = NATIVE_GET_LOCAL(0);
48 if ((OBJ_GET_TYPE(pa) != OBJ_TYPE_STR) || (NATIVE_GET_NUM_ARGS() < 1)
49 || (NATIVE_GET_NUM_ARGS() > 2))
51 PM_RAISE(retval, PM_RET_EX_TYPE);
52 return retval;
55 /* Get the base, if it exists; otherwise assume 10 */
56 base = 10;
57 if (NATIVE_GET_NUM_ARGS() == 2)
59 pb = NATIVE_GET_LOCAL(1);
61 /* Raise a TypeError if 2nd arg is not an int */
62 if (OBJ_GET_TYPE(pb) != OBJ_TYPE_INT)
64 PM_RAISE(retval, PM_RET_EX_TYPE);
65 return retval;
68 base = ((pPmInt_t)pb)->val;
70 /* Raise ValueError if base is out of range */
71 if ((base < 0) || (base == 1) || (base > 36))
73 PM_RAISE(retval, PM_RET_EX_VAL);
74 return retval;
78 /* Perform conversion */
79 pend = C_NULL;
80 pc = (char const *)&(((pPmString_t)pa)->val);
81 i = strtol(pc, &pend, base);
83 /* Raise ValueError if there was a conversion error */
84 if (*pend != C_NULL)
86 PM_RAISE(retval, PM_RET_EX_VAL);
87 return retval;
90 /* Create an int object to hold the result of the conversion */
91 retval = int_new(i, &pi);
93 NATIVE_SET_TOS(pi);
95 return retval;
96 """
97 pass
101 # Returns the number of occurrences of substring s2 in string s1.
102 # WARNING: Does not match Python's behavior if s1 contains a null character.
104 def count(s1, s2):
105 """__NATIVE__
106 pPmObj_t ps1;
107 pPmObj_t ps2;
108 uint8_t *pc1;
109 uint8_t *pc2;
110 uint8_t *pscan;
111 uint8_t *pmatch;
112 uint8_t pc2c0;
113 uint16_t pc1len;
114 uint16_t pc2len;
115 uint16_t n;
116 uint16_t remaining;
117 uint16_t cmp;
118 pPmObj_t pn;
119 PmReturn_t retval = PM_RET_OK;
121 /* Raise TypeError if it's not a string or wrong number of args, */
122 ps1 = NATIVE_GET_LOCAL(0);
123 ps2 = NATIVE_GET_LOCAL(1);
124 if ((OBJ_GET_TYPE(ps1) != OBJ_TYPE_STR) || (NATIVE_GET_NUM_ARGS() != 2)
125 || (OBJ_GET_TYPE(ps2) != OBJ_TYPE_STR))
127 PM_RAISE(retval, PM_RET_EX_TYPE);
128 return retval;
131 pc1 = ((pPmString_t)ps1)->val;
132 pc2 = ((pPmString_t)ps2)->val;
133 pc1len = ((pPmString_t)ps1)->length;
134 pc2len = ((pPmString_t)ps2)->length;
135 n = 0;
137 /* Handle some quick special cases (order of if-clauses is important) */
138 if (pc2len == 0)
140 n = pc1len + 1;
142 else if (pc1len == 0)
144 n = 0;
147 /* Count the number of matches */
148 else
150 n = 0;
151 remaining = pc1len;
152 pscan = pc1;
153 pc2c0 = pc2[0];
154 while (pscan <= (pc1 + (pc1len - pc2len)))
156 /* Find the next possible start */
157 pmatch = (uint8_t *)memchr(pscan, pc2c0, remaining);
158 if (pmatch == C_NULL) break;
159 remaining -= (pmatch - pscan);
160 pscan = pmatch;
162 /* If it matches, increase the count, else try the next char */
163 cmp = memcmp(pscan, pc2, pc2len);
164 if (cmp == 0)
166 n++;
167 pscan += pc2len;
168 remaining -= pc2len;
170 else
172 pscan++;
173 remaining--;
178 retval = int_new(n, &pn);
180 NATIVE_SET_TOS(pn);
182 return retval;
184 pass
188 # Returns the lowest index in s1 where substring s2 is found or -1 on failure.
189 # WARNING: Does not accept optional start,end arguments.
191 def find(s1, s2):
192 """__NATIVE__
193 pPmObj_t ps1;
194 pPmObj_t ps2;
195 uint8_t *pc1;
196 uint8_t *pc2;
197 uint8_t *pmatch;
198 uint8_t pc1len;
199 uint8_t pc2len;
200 int32_t n;
201 pPmObj_t pn;
202 PmReturn_t retval = PM_RET_OK;
204 /* Raise TypeError if it's not a string or wrong number of args, */
205 ps1 = NATIVE_GET_LOCAL(0);
206 ps2 = NATIVE_GET_LOCAL(1);
207 if ((OBJ_GET_TYPE(ps1) != OBJ_TYPE_STR) || (NATIVE_GET_NUM_ARGS() != 2)
208 || (OBJ_GET_TYPE(ps2) != OBJ_TYPE_STR))
210 PM_RAISE(retval, PM_RET_EX_TYPE);
211 return retval;
214 pc1 = ((pPmString_t)ps1)->val;
215 pc2 = ((pPmString_t)ps2)->val;
216 pc1len = ((pPmString_t)ps1)->length;
217 pc2len = ((pPmString_t)ps2)->length;
218 n = -1;
220 /* Handle a quick special case */
221 if (pc2len == 0)
223 n = 0;
226 /* Try to find the index of the substring */
227 else
229 /* Find the next possible start */
230 pmatch = (uint8_t *)memchr(pc1, pc2[0], pc1len);
231 if (pmatch != C_NULL)
233 /* If it matches, calculate the index */
234 if (memcmp(pmatch, pc2, pc2len) == 0)
236 n = pmatch - pc1;
241 retval = int_new(n, &pn);
243 NATIVE_SET_TOS(pn);
245 return retval;
247 pass
250 def join(s, sep=' '):
251 len_s = len(s)
252 if len_s == 0:
253 return ''
254 rs = s[0]
255 i = 1
256 while i < len_s:
257 rs = rs + sep + s[i]
258 i += 1
259 return rs
262 # :mode=c: