Strings.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // Copyright (c) 2001 Per M.A. Bothner and Brainfood Inc.
  2. // This is free software; for terms and warranty disclaimer see ./COPYING.
  3. package gnu.lists;
  4. import gnu.text.Char;
  5. import java.io.IOException;
  6. /** Various static utility methods for general strings (CharSeqs). */
  7. public class Strings
  8. {
  9. /** Get character (code point) at a offset.
  10. * @param index offset measured in 16-bit code units
  11. */
  12. public static int characterAt(CharSequence cseq, int index) {
  13. return characterAt(cseq, 0, cseq.length(), index);
  14. }
  15. /** Get character (code point) at a offset.
  16. * @param index offset measured in 16-bit code units,
  17. * from begining of cseq, not frm start
  18. */
  19. public static int characterAt(CharSequence cseq, int start, int end,
  20. int index) {
  21. if (index < start || index >= end)
  22. throw new IndexOutOfBoundsException();
  23. char ch1 = cseq.charAt(index);
  24. if (ch1 >= 0xD800 && ch1 <= 0xDBFF) {
  25. if (index + 1 < end) {
  26. char ch2 = cseq.charAt(index+1);
  27. if (ch2 >= 0xDC00 && ch2 <= 0xDFFF)
  28. return ((ch1 - 0xD800) << 10) + (ch2 - 0xDC00) + 0x10000;
  29. }
  30. } else if (ch1 >= 0xDC00 && ch1 <= 0xDFFF) {
  31. if (index > start) {
  32. char ch0 = cseq.charAt(index-1);
  33. if (ch0 >= 0xD800 && ch0 <= 0xDBFF)
  34. return Char.IGNORABLE_CHAR;
  35. }
  36. }
  37. return ch1;
  38. }
  39. /** Get index'th character (code point).
  40. * @param index offset by code points
  41. */
  42. public static int indexByCodePoints(CharSequence str, int index) {
  43. if (str instanceof IString)
  44. return ((IString) str).indexByCodePoints(index);
  45. index = Character.offsetByCodePoints(str, 0, index);
  46. return Character.codePointAt(str, index);
  47. }
  48. /** Like offsetByCodePoints, but optimize if an IString.
  49. * @param offset number of code points beyond start index.
  50. * @param cuStart start index in code units (Java chars)
  51. * @param cpStart start index in Unicode code points
  52. */
  53. public static int offsetByCodePoints(CharSequence str, int offset,
  54. int cuStart, int cpStart) {
  55. if (str instanceof IString) {
  56. IString istr = (IString) str;
  57. offset += cpStart;
  58. if (offset < 0 || offset > istr.size())
  59. throw new IndexOutOfBoundsException();
  60. return istr.offsetByCodePoints(offset);
  61. }
  62. return Character.offsetByCodePoints(str, cuStart, offset);
  63. }
  64. public static int sizeInCodePoints(CharSequence str) {
  65. if (str instanceof IString)
  66. return ((IString) str).lengthByCodePoints();
  67. int len = str.length();
  68. int nsurr = 0;
  69. for (int i = 0; i < len; ) {
  70. char ch = str.charAt(i++);
  71. if (ch >= 0xD800 && ch <= 0xDBFF && i < len) {
  72. int next = str.charAt(i);
  73. if (next >= 0xDC00 && next <= 0xDFFF) {
  74. i++;
  75. nsurr++;
  76. }
  77. }
  78. }
  79. return len-nsurr;
  80. }
  81. /** Change every character to be uppercase. */
  82. public static void makeUpperCase(CharSeq str)
  83. {
  84. for (int i = str.length(); --i >= 0; )
  85. str.setCharAt(i, Character.toUpperCase(str.charAt(i)));
  86. }
  87. /** Change every character to be lowercase. */
  88. public static void makeLowerCase(CharSeq str)
  89. {
  90. for (int i = str.length(); --i >= 0; )
  91. str.setCharAt(i, Character.toLowerCase(str.charAt(i)));
  92. }
  93. /** Capitalize this string.
  94. * Change first character of each word to titlecase,
  95. * and change the other characters to lowercase. */
  96. public static void makeCapitalize(CharSeq str)
  97. {
  98. char prev = ' ';
  99. int len = str.length();
  100. for (int i = 0; i < len; i++)
  101. {
  102. char ch = str.charAt(i);
  103. if (! Character.isLetterOrDigit(prev))
  104. ch = Character.toTitleCase(ch);
  105. else
  106. ch = Character.toLowerCase(ch);
  107. str.setCharAt(i, ch);
  108. prev = ch;
  109. }
  110. }
  111. public static String toJson(CharSequence str) {
  112. StringBuilder sbuf = new StringBuilder();
  113. printQuoted(str, sbuf, 3);
  114. return sbuf.toString();
  115. }
  116. public static void printJson(CharSequence str, Appendable ps) {
  117. printQuoted(str, ps, 3);
  118. }
  119. /** Print a string with quotes and escapes.
  120. * @param escapes The value 0 means only escape '"' and '\\';
  121. * the value 1 means escape standard escape characters like '\\b';
  122. * the value 2 means escape all non-ascii or control characters;
  123. * the value 3 means follow the JSON standard.
  124. */
  125. public static void printQuoted(CharSequence str,
  126. Appendable ps, int escapes) {
  127. int len = str.length();
  128. try {
  129. ps.append('\"');
  130. for (int i = 0; i < len; i++) {
  131. char ch = str.charAt(i);
  132. if ((ch == '\\' || ch == '\"'))
  133. ps.append('\\');
  134. else if (escapes > 0) {
  135. // These escapes are R6RS:
  136. if (ch == '\n')
  137. { ps.append("\\n"); continue; }
  138. else if (ch == '\r')
  139. { ps.append("\\r"); continue; }
  140. else if (ch == '\t')
  141. { ps.append("\\t"); continue; }
  142. else if (ch == '\007' && escapes < 3)
  143. { ps.append("\\a"); continue; }
  144. else if (ch == '\b')
  145. { ps.append("\\b"); continue; }
  146. else if (ch == '\013' && escapes < 3)
  147. { ps.append("\\v"); continue; }
  148. else if (ch == '\f')
  149. { ps.append("\\f"); continue; }
  150. else if (escapes >= 3 && (ch < ' ' || ch >= 127))
  151. {
  152. ps.append("\\u");
  153. int d = ch;
  154. for (int k = 12; k >= 0; k -= 4) {
  155. ps.append(Character.forDigit((d >> k) & 15, 16));
  156. }
  157. continue;
  158. }
  159. else if (ch < ' ' || (escapes > 1 && ch >= 127))
  160. {
  161. ps.append("\\x");
  162. ps.append(Integer.toHexString(ch));
  163. ps.append(';');
  164. continue;
  165. }
  166. }
  167. ps.append(ch);
  168. }
  169. ps.append('\"');
  170. } catch (IOException ex) {
  171. throw new RuntimeException(ex);
  172. }
  173. }
  174. public static void copyInto(CharSequence src, int start, int end,
  175. CharSeq dst, int at) {
  176. int dstLen = dst.length();
  177. int srcLen = src.length();
  178. if (at < 0 || at > dstLen || start < 0 || end > srcLen || end < start
  179. || dstLen - at < end - start)
  180. throw new StringIndexOutOfBoundsException();
  181. if (at < start) {
  182. int i = at;
  183. int j = start;
  184. for (; j < end; i++, j++) {
  185. dst.setCharAt(i, src.charAt(j));
  186. }
  187. }
  188. else {
  189. int i = at + end - start;
  190. int j = end;
  191. while (--j >= start) {
  192. dst.setCharAt(--i, src.charAt(j));
  193. }
  194. }
  195. }
  196. /** Make a read-only substring, generalized to arbitrary index sequences.
  197. * The indexes are in terms of code points (character) offsets.
  198. */
  199. public static IString indirectIndexed(CharSequence base,
  200. IntSequence indexes) {
  201. if (indexes instanceof Range.IntRange) {
  202. Range.IntRange range = (Range.IntRange) indexes;
  203. if (range.getStepInt() == 1) {
  204. int start = range.getStartInt();
  205. int end = base.length();
  206. if (start < 0 || start > end)
  207. throw new IndexOutOfBoundsException();
  208. int size;
  209. if (! range.isUnbounded()) {
  210. size = range.size();
  211. if (start+size < 0 || start+size > end)
  212. throw new IndexOutOfBoundsException();
  213. } else
  214. size = end - start;
  215. return IString.valueOf(base, start, size);
  216. }
  217. }
  218. int len = indexes.size();
  219. StringBuilder sbuf = new StringBuilder(len);
  220. for (int i = 0; i < len; i++) {
  221. int ch = Strings.indexByCodePoints(base, indexes.getInt(i));
  222. if (ch >= 0x10000) {
  223. sbuf.append((char) (((ch - 0x10000) >> 10) + 0xD800));
  224. ch = (ch & 0x3FF) + 0xDC00;
  225. }
  226. sbuf.append((char) ch);
  227. }
  228. return new IString(sbuf.toString());
  229. }
  230. /** Make a read-only substring.
  231. * The start and end are in terms of code unit (16-bit char).
  232. */
  233. public static CharSequence substring(CharSequence base,
  234. int start, int end) {
  235. if (base instanceof FString) {
  236. FString fstr = (FString) base;
  237. if (fstr.isVerySimple() || fstr.isSubRange())
  238. return (CharSequence) Sequences.copySimple(fstr, start, end, false);
  239. }
  240. if (base instanceof String) {
  241. return ((String) base).substring(start, end);
  242. } else {
  243. int len = end - start;
  244. StringBuilder sbuf = new StringBuilder(len);
  245. if (base instanceof CharSeq) {
  246. try {
  247. ((CharSeq) base).writeTo(start, len, sbuf);
  248. } catch (Throwable ex) {
  249. throw new RuntimeException(ex);
  250. }
  251. } else {
  252. for (int i = start; i < end; i++)
  253. sbuf.append(base.charAt(i));
  254. }
  255. return sbuf.toString();
  256. }
  257. }
  258. public static String fromUtf8(byte[] bytes, int start, int length) {
  259. /* #ifdef JAVA7 */
  260. return new String(bytes, start, length, java.nio.charset.StandardCharsets.UTF_8);
  261. /* #else */
  262. // try {
  263. // return new String(bytes, start, length, "UTF-8");
  264. // } catch (java.io.UnsupportedEncodingException ex) {
  265. // throw new RuntimeException(ex);
  266. // }
  267. /* #endif */
  268. }
  269. public static byte[] toUtf16(CharSequence str, int start, int end,
  270. boolean bigEndian, boolean writeBOM) {
  271. int blen = 2*(end-start)+(writeBOM?2:0);
  272. byte[] buf = new byte[blen];
  273. int hi = bigEndian ? 0 : 1;
  274. int lo = bigEndian ? 1 : 0;
  275. int i = start;
  276. int j = 0;
  277. while (j < blen) {
  278. char ch;
  279. if (writeBOM) {
  280. ch = '\uFEFF';
  281. writeBOM = false;
  282. }
  283. else
  284. ch = str.charAt(i++);
  285. buf[j + lo] = (byte) ch;
  286. buf[j + hi] = (byte) (ch >> 8);
  287. j += 2;
  288. }
  289. return buf;
  290. }
  291. public static int compareTo(CharSequence str1, CharSequence str2) {
  292. int n1 = str1.length();
  293. int n2 = str2.length();
  294. int n = n1 > n2 ? n2 : n1;
  295. for (int i = 0; i < n; i++) {
  296. char c1 = str1.charAt(i);
  297. char c2 = str2.charAt(i);
  298. int d = c1 - c2;
  299. if (d != 0)
  300. return d;
  301. }
  302. return n1 - n2;
  303. }
  304. public static String replicate(int from, int to, boolean suppliedTo,
  305. CharSequence string,
  306. int start, int end, boolean suppliedEnd) {
  307. int sstart = Strings.offsetByCodePoints(string, start, 0, 0);
  308. if (end <= start || (suppliedTo && to < from)) {
  309. if (end >= start && from == to)
  310. return "";
  311. throw new StringIndexOutOfBoundsException();
  312. }
  313. int slen = end - start;
  314. // startOffset = modulo(from, slen)
  315. int startOffset = from % slen;
  316. if (startOffset < 0) startOffset += slen;
  317. int ptr = Strings.offsetByCodePoints(string, startOffset,
  318. sstart, start);
  319. int send = ! suppliedEnd ? string.length()
  320. : Strings.offsetByCodePoints(string, end-startOffset, ptr, startOffset);
  321. StringBuilder buf = new StringBuilder();
  322. for (int i = from;
  323. suppliedTo ? i < to : ptr < send;
  324. i++) {
  325. if (ptr == send)
  326. ptr = sstart;
  327. char ch = string.charAt(ptr);
  328. ptr++;
  329. buf.append(ch);
  330. if (ch >= 0xD800 && ch <= 0xDBFF && ptr < send) {
  331. char next = string.charAt(ptr);
  332. if (next >= 0xDC00 && next <= 0xDFFF) {
  333. ptr++;
  334. buf.append(next);
  335. }
  336. }
  337. }
  338. return buf.toString();
  339. }
  340. }