Changeset 1349
- Timestamp:
- 11/29/09 20:10:18 (2 years ago)
- Files:
-
- trunk/phobos/std/string.d (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/phobos/std/string.d
r1319 r1349 1841 1841 r[width - s.length .. width] = s; 1842 1842 return assumeUnique(r); 1843 1843 } 1844 1844 1845 1845 /******************************************** 1846 1846 * Replace occurrences of from[] with to[] in s[]. 1847 1847 */ 1848 1848 1849 1849 string replace(string s, string from, string to) 1850 1850 { 1851 if (from.length == 0) return s; 1852 1851 1853 char[] p; 1852 int i; 1853 size_t istart; 1854 1855 //printf("replace('%.*s','%.*s','%.*s')\n", s, from, to); 1856 if (from.length == 0) 1857 return s; 1858 istart = 0; 1859 while (istart < s.length) 1860 { 1861 i = indexOf(s[istart .. s.length], from); 1854 for (size_t istart; istart < s.length; ) 1855 { 1856 auto i = indexOf(s[istart .. s.length], from); 1862 1857 if (i == -1) 1863 1858 { 1859 if (istart == 0) 1860 { 1861 // Never found, so just return s 1862 return s; 1863 } 1864 1864 p ~= s[istart .. s.length]; 1865 1865 break; 1866 1866 } 1867 1867 p ~= s[istart .. istart + i]; 1868 1868 p ~= to; 1869 1869 istart += i + from.length; 1870 1870 } 1871 1871 return assumeUnique(p); 1872 1872 } 1873 1873 … … 1881 1881 string r; 1882 1882 int i; 1883 1883 1884 1884 r = replace(s, from, to); 1885 1885 i = cmp(r, "This is a silly silly list"); 1886 1886 assert(i == 0); 1887 1887 1888 1888 r = replace(s, "", to); 1889 1889 i = cmp(r, "This is a foo foo list"); 1890 1890 assert(i == 0); 1891 1892 assert(replace(r, "won't find this", "whatever") is r); 1891 1893 } 1892 1894 1893 1895 /***************************** 1894 1896 * Return a _string that is s[] with slice[] replaced by replacement[]. 1895 1897 */ 1896 1898 1897 1899 string replaceSlice(string s, in string slice, in string replacement) 1898 1900 in 1899 1901 { 1900 1902 // Verify that slice[] really is a slice of s[]
