Mercurial > urweb
comparison src/c/urweb.c @ 829:20fe00fd81da
Substring functions; fix a nasty MonoReduce pattern match substitution bug
author | Adam Chlipala <adamc@hcoop.net> |
---|---|
date | Sat, 30 May 2009 13:29:00 -0400 |
parents | 14a6c0971d89 |
children | 5e1a4b12c83a |
comparison
equal
deleted
inserted
replaced
828:14a6c0971d89 | 829:20fe00fd81da |
---|---|
1809 | 1809 |
1810 uw_Basis_int uw_Basis_strlen(uw_context ctx, uw_Basis_string s) { | 1810 uw_Basis_int uw_Basis_strlen(uw_context ctx, uw_Basis_string s) { |
1811 return strlen(s); | 1811 return strlen(s); |
1812 } | 1812 } |
1813 | 1813 |
1814 uw_Basis_string uw_Basis_strchr(uw_context ctx, uw_Basis_string s, uw_Basis_char ch) { | |
1815 return strchr(s, ch); | |
1816 } | |
1817 | |
1818 uw_Basis_int *uw_Basis_strindex(uw_context ctx, uw_Basis_string s, uw_Basis_char ch) { | |
1819 uw_Basis_string r = strchr(s, ch); | |
1820 if (r == NULL) | |
1821 return NULL; | |
1822 else { | |
1823 uw_Basis_int *nr = uw_malloc(ctx, sizeof(uw_Basis_int)); | |
1824 *nr = r - s; | |
1825 return nr; | |
1826 } | |
1827 } | |
1828 | |
1814 uw_Basis_string uw_Basis_strcat(uw_context ctx, uw_Basis_string s1, uw_Basis_string s2) { | 1829 uw_Basis_string uw_Basis_strcat(uw_context ctx, uw_Basis_string s1, uw_Basis_string s2) { |
1815 int len = uw_Basis_strlen(ctx, s1) + uw_Basis_strlen(ctx, s2) + 1; | 1830 int len = uw_Basis_strlen(ctx, s1) + uw_Basis_strlen(ctx, s2) + 1; |
1816 char *s; | 1831 char *s; |
1817 | 1832 |
1818 uw_check_heap(ctx, len); | 1833 uw_check_heap(ctx, len); |
1822 strcpy(s, s1); | 1837 strcpy(s, s1); |
1823 strcat(s, s2); | 1838 strcat(s, s2); |
1824 ctx->heap.front += len; | 1839 ctx->heap.front += len; |
1825 | 1840 |
1826 return s; | 1841 return s; |
1842 } | |
1843 | |
1844 uw_Basis_string uw_Basis_substring(uw_context ctx, uw_Basis_string s, uw_Basis_int start, uw_Basis_int len) { | |
1845 size_t full_len = uw_Basis_strlen(ctx, s); | |
1846 | |
1847 if (start < 0) | |
1848 uw_error(ctx, FATAL, "substring: Negative start index"); | |
1849 if (len < 0) | |
1850 uw_error(ctx, FATAL, "substring: Negative length"); | |
1851 if (start + len > full_len) | |
1852 uw_error(ctx, FATAL, "substring: Start index plus length is too large"); | |
1853 | |
1854 if (start + len == full_len) | |
1855 return &s[start]; | |
1856 else { | |
1857 uw_Basis_string r = uw_malloc(ctx, len+1); | |
1858 memcpy(r, s, len); | |
1859 r[len] = 0; | |
1860 return r; | |
1861 } | |
1862 | |
1827 } | 1863 } |
1828 | 1864 |
1829 uw_Basis_string uw_strdup(uw_context ctx, uw_Basis_string s1) { | 1865 uw_Basis_string uw_strdup(uw_context ctx, uw_Basis_string s1) { |
1830 int len = uw_Basis_strlen(ctx, s1) + 1; | 1866 int len = uw_Basis_strlen(ctx, s1) + 1; |
1831 char *s; | 1867 char *s; |