Reverses this CharArray, keeping surrogate pairs together.
()
| 1869 | |
| 1870 | /** Reverses this CharArray, keeping surrogate pairs together. */ |
| 1871 | public CharArray reverseCodePoints () { |
| 1872 | if (size < 2) return this; |
| 1873 | int end = size - 1; |
| 1874 | char frontHigh = items[0]; |
| 1875 | char endLow = items[end]; |
| 1876 | boolean allowFrontSur = true, allowEndSur = true; |
| 1877 | for (int i = 0, mid = size / 2; i < mid; i++, --end) { |
| 1878 | char frontLow = items[i + 1]; |
| 1879 | char endHigh = items[end - 1]; |
| 1880 | boolean surAtFront = allowFrontSur && frontLow >= 0xdc00 && frontLow <= 0xdfff && frontHigh >= 0xd800 |
| 1881 | && frontHigh <= 0xdbff; |
| 1882 | if (surAtFront && size < 3) return this; |
| 1883 | boolean surAtEnd = allowEndSur && endHigh >= 0xd800 && endHigh <= 0xdbff && endLow >= 0xdc00 && endLow <= 0xdfff; |
| 1884 | allowFrontSur = allowEndSur = true; |
| 1885 | if (surAtFront == surAtEnd) { |
| 1886 | if (surAtFront) { // both surrogates |
| 1887 | items[end] = frontLow; |
| 1888 | items[end - 1] = frontHigh; |
| 1889 | items[i] = endHigh; |
| 1890 | items[i + 1] = endLow; |
| 1891 | frontHigh = items[i + 2]; |
| 1892 | endLow = items[end - 2]; |
| 1893 | i++; |
| 1894 | end--; |
| 1895 | } else { // neither surrogates |
| 1896 | items[end] = frontHigh; |
| 1897 | items[i] = endLow; |
| 1898 | frontHigh = frontLow; |
| 1899 | endLow = endHigh; |
| 1900 | } |
| 1901 | } else if (surAtFront) { // surrogate only at the front |
| 1902 | items[end] = frontLow; |
| 1903 | items[i] = endLow; |
| 1904 | endLow = endHigh; |
| 1905 | allowFrontSur = false; |
| 1906 | } else { // surrogate only at the end |
| 1907 | items[end] = frontHigh; |
| 1908 | items[i] = endHigh; |
| 1909 | frontHigh = frontLow; |
| 1910 | allowEndSur = false; |
| 1911 | } |
| 1912 | } |
| 1913 | if ((size & 1) == 1 && (!allowFrontSur || !allowEndSur)) items[end] = allowFrontSur ? endLow : frontHigh; |
| 1914 | return this; |
| 1915 | } |
| 1916 | |
| 1917 | /** Extracts the rightmost characters from this CharArray without throwing an exception. |
| 1918 | * <p> |