Mercurial > urweb
comparison src/c/urweb.c @ 821:395a5d450cc0
Chars and more string operations
author | Adam Chlipala <adamc@hcoop.net> |
---|---|
date | Tue, 26 May 2009 12:25:06 -0400 |
parents | 5f49a6b759cb |
children | 14a6c0971d89 |
comparison
equal
deleted
inserted
replaced
820:91f465ded07e | 821:395a5d450cc0 |
---|---|
1791 } | 1791 } |
1792 | 1792 |
1793 return uw_unit_v; | 1793 return uw_unit_v; |
1794 } | 1794 } |
1795 | 1795 |
1796 uw_Basis_char uw_Basis_strsub(uw_context ctx, uw_Basis_string s, uw_Basis_int n) { | |
1797 if (n >= 0 && n < strlen(s)) | |
1798 return s[n]; | |
1799 else | |
1800 uw_error(ctx, FATAL, "Out-of-bounds strsub"); | |
1801 } | |
1802 | |
1803 uw_Basis_string uw_Basis_strsuffix(uw_context ctx, uw_Basis_string s, uw_Basis_int n) { | |
1804 if (n >= 0 && n < strlen(s)) | |
1805 return &s[n]; | |
1806 else | |
1807 uw_error(ctx, FATAL, "Out-of-bounds strsuffix"); | |
1808 } | |
1809 | |
1796 uw_Basis_string uw_Basis_strcat(uw_context ctx, uw_Basis_string s1, uw_Basis_string s2) { | 1810 uw_Basis_string uw_Basis_strcat(uw_context ctx, uw_Basis_string s1, uw_Basis_string s2) { |
1797 int len = strlen(s1) + strlen(s2) + 1; | 1811 int len = strlen(s1) + strlen(s2) + 1; |
1798 char *s; | 1812 char *s; |
1799 | 1813 |
1800 uw_check_heap(ctx, len); | 1814 uw_check_heap(ctx, len); |
2079 sprintf(r, "%g%n", n, &len); | 2093 sprintf(r, "%g%n", n, &len); |
2080 ctx->heap.front += len+1; | 2094 ctx->heap.front += len+1; |
2081 return r; | 2095 return r; |
2082 } | 2096 } |
2083 | 2097 |
2098 uw_Basis_string uw_Basis_charToString(uw_context ctx, uw_Basis_char ch) { | |
2099 char *r = uw_malloc(ctx, 2); | |
2100 r[0] = ch; | |
2101 r[1] = 0; | |
2102 return r; | |
2103 } | |
2104 | |
2084 uw_Basis_string uw_Basis_boolToString(uw_context ctx, uw_Basis_bool b) { | 2105 uw_Basis_string uw_Basis_boolToString(uw_context ctx, uw_Basis_bool b) { |
2085 if (b == uw_Basis_False) | 2106 if (b == uw_Basis_False) |
2086 return "False"; | 2107 return "False"; |
2087 else | 2108 else |
2088 return "True"; | 2109 return "True"; |
2125 return r; | 2146 return r; |
2126 } else | 2147 } else |
2127 return NULL; | 2148 return NULL; |
2128 } | 2149 } |
2129 | 2150 |
2151 uw_Basis_char *uw_Basis_stringToChar(uw_context ctx, uw_Basis_string s) { | |
2152 if (s[0] == 0) { | |
2153 uw_Basis_char *r = uw_malloc(ctx, 1); | |
2154 r[0] = 0; | |
2155 return r; | |
2156 } else if (s[1] != 0) | |
2157 return NULL; | |
2158 else { | |
2159 uw_Basis_char *r = uw_malloc(ctx, 1); | |
2160 r[0] = s[0]; | |
2161 return r; | |
2162 } | |
2163 } | |
2164 | |
2130 uw_Basis_bool *uw_Basis_stringToBool(uw_context ctx, uw_Basis_string s) { | 2165 uw_Basis_bool *uw_Basis_stringToBool(uw_context ctx, uw_Basis_string s) { |
2131 static uw_Basis_bool true = uw_Basis_True; | 2166 static uw_Basis_bool true = uw_Basis_True; |
2132 static uw_Basis_bool false = uw_Basis_False; | 2167 static uw_Basis_bool false = uw_Basis_False; |
2133 | 2168 |
2134 if (!strcasecmp (s, "True")) | 2169 if (!strcasecmp (s, "True")) |
2211 | 2246 |
2212 if (*s != '\0' && *endptr == '\0') | 2247 if (*s != '\0' && *endptr == '\0') |
2213 return n; | 2248 return n; |
2214 else | 2249 else |
2215 uw_error(ctx, FATAL, "Can't parse float: %s", s); | 2250 uw_error(ctx, FATAL, "Can't parse float: %s", s); |
2251 } | |
2252 | |
2253 uw_Basis_char uw_Basis_stringToChar_error(uw_context ctx, uw_Basis_string s) { | |
2254 if (s[0] == 0) | |
2255 return 0; | |
2256 else if (s[1] != 0) | |
2257 uw_error(ctx, FATAL, "Can't parse char: %s", s); | |
2258 else | |
2259 return s[0]; | |
2216 } | 2260 } |
2217 | 2261 |
2218 uw_Basis_bool uw_Basis_stringToBool_error(uw_context ctx, uw_Basis_string s) { | 2262 uw_Basis_bool uw_Basis_stringToBool_error(uw_context ctx, uw_Basis_string s) { |
2219 if (!strcasecmp(s, "T") || !strcasecmp (s, "True")) | 2263 if (!strcasecmp(s, "T") || !strcasecmp (s, "True")) |
2220 return uw_Basis_True; | 2264 return uw_Basis_True; |