public static CharArray getAllChars() { CharArray array = new CharArray(); for (int i = 0; i < chars.length; i++) if (!array.contains(IndividualChar.getChar(i))) array.add(IndividualChar.getChar(i)); for (int i = 0; i < chars.length; i++) if (!array.contains(StartChar.getChar(i))) array.add(StartChar.getChar(i)); for (int i = 0; i < chars.length; i++) if (!array.contains(CenterChar.getChar(i))) array.add(CenterChar.getChar(i)); for (int i = 0; i < chars.length; i++) if (!array.contains(EndChar.getChar(i))) array.add(EndChar.getChar(i)); return array; }
public MutableString(CharArray storage, CharSequence string) { this.storage = storage; if (string == null) return; ptr = storage.size; length = string.length(); if (string instanceof MutableString) { MutableString mutableString = (MutableString) string; if (storage == mutableString.storage) { //same storage, so copy pointer ptr = mutableString.ptr; } else { // add char[] this.storage.addAll(mutableString.storage.items, mutableString.ptr, length); } } else if (string instanceof String) { char[] arr = ((String) string).toCharArray(); this.storage.addAll(arr); } else { //copy char by char for (int i = 0; i < length; i++) { this.storage.add(string.charAt(i)); } } }
@Test void constructor() { CharArray buffer = new CharArray(); CharArray buffer2 = new CharArray(); String t1 = "TestString"; StringBuilder sb = new StringBuilder(); sb.append(t1); MutableString mutable1 = new MutableString(buffer, t1); MutableString mutable2 = new MutableString(buffer2, mutable1); MutableString mutable3 = new MutableString(buffer, sb); assertThat("MutableString must equals", CharSequenceUtil.equals(t1, mutable1)); assertThat("MutableString must equals", CharSequenceUtil.equals(t1, mutable2)); assertThat("MutableString must equals", CharSequenceUtil.equals(t1, mutable3)); assertThat("MutableString must equals", CharSequenceUtil.equals(mutable1, mutable2)); assertThat("MutableString must equals", CharSequenceUtil.equals(mutable1, mutable3)); assertThat("MutableString must equals", CharSequenceUtil.equals(mutable2, mutable3)); }
@Override public void write(Kryo kryo, Output output, CharArray array) { output.writeVarInt(array.size, true); output.writeBoolean(array.ordered); for (int i = 0; i < array.size; i++) { output.writeChar(array.get(i)); } }
@Override public CharArray read(Kryo kryo, Input input, Class<CharArray> type) { int length = input.readVarInt(true); boolean ordered = input.readBoolean(); CharArray array = new CharArray(ordered, length); for (int i = 0; i < length; i++) { array.add(input.readChar()); } return array; }
private String getText() { CharArray ltrChars = new CharArray(); String text = ""; // add only LTR characters into array. for (int i = glyphs.size - 1; i >= 0; i--) { if (glyphs.get(i).isRTL()) break; ltrChars.add(glyphs.get(i).getOriginalChar()); } // fix space between RTL and LTR characters. fixSpace(ltrChars); // reverse LTR characters and update main glyphs. for (int i = glyphs.size - 1; i >= 0; i--) { if (glyphs.get(i).isRTL()) break; glyphs.get(i).setChar(ltrChars.pop()); } // append glyphs together. for (int i = glyphs.size - 1; i >= 0; i--) { char c = glyphs.get(i).getChar(); text += c == KeyEvent.KEY_LOCATION_UNKNOWN ? KeyEvent.VK_SPACE : c; } return text; }
/** * This method can fix approximately the space between RTL and LTR characters. * * @param ltrChars contains LTR characters ONLY. */ private void fixSpace(CharArray ltrChars) { if (ltrChars.size > 0) if (ltrChars.first() == KeyEvent.VK_SPACE || ltrChars.first() == KeyEvent.KEY_LOCATION_UNKNOWN) { if (ltrChars.first() == KeyEvent.VK_SPACE && ltrChars.peek() == KeyEvent.VK_SPACE) return; ltrChars.add(ltrChars.removeIndex(0)); } else if (ltrChars.peek() == KeyEvent.VK_SPACE || ltrChars.peek() == KeyEvent.KEY_LOCATION_UNKNOWN) ltrChars.insert(0, ltrChars.pop()); }
public ParameterStringSequence(CharArray storage, String string) { this.store = storage; int pos = string.indexOf("{"); if (pos < 0) { sequence = new MutableString(this.store, string); } else { sequence = new MutableString(this.store, string.substring(0, pos)); do { int pos2 = string.indexOf("}", pos) + 1; String para = string.substring(pos, pos2); ReplacedStringSequence rss = new ReplacedStringSequence(para); replaceArray.add(rss); sequence.add(rss); pos = string.indexOf("{", pos2); if (pos < 0) { // add last and break sequence.add(new MutableString(this.store, string.substring(pos2, string.length()))); break; } sequence.add(new MutableString(this.store, string.substring(pos2, pos))); } while (pos >= 0); replaceArray.sort(new Comparator<ReplacedStringSequence>() { @Override public int compare(ReplacedStringSequence o1, ReplacedStringSequence o2) { String s1 = o1.toString().replace("{", "").replace("}", ""); String s2 = o2.toString().replace("{", "").replace("}", ""); int i1 = s1.isEmpty() ? 0 : Integer.parseInt(s1); int i2 = s2.isEmpty() ? 0 : Integer.parseInt(s2); return i1 - i2; } }); } }
@Test void length() { CharArray buffer = new CharArray(); String t1 = "TestString"; MutableString mutable1 = new MutableString(buffer, t1); MutableString mutable2 = new MutableString(buffer, mutable1); assertThat("Length must: " + t1.length() + "but was: " + mutable1.length(), t1.length() == mutable1.length()); assertThat("Length must: " + t1.length() + "but was: " + mutable2.length(), t1.length() == mutable2.length()); }
@Test void charAt() { CharArray buffer = new CharArray(); String t1 = "Test\nString"; MutableString mutable1 = new MutableString(buffer, t1); MutableString mutable2 = new MutableString(buffer, mutable1); assertThat("MutableString must equals", CharSequenceUtil.equals(t1, mutable1)); assertThat("MutableString must equals", CharSequenceUtil.equals(t1, mutable2)); assertThat("MutableString must equals", CharSequenceUtil.equals(mutable1, mutable2)); }
@Test void subSequence() { CharArray buffer = new CharArray(); String t1 = "TestString"; CharSequence sub = t1.subSequence(4, 10); MutableString mutable1 = new MutableString(buffer, t1); MutableString mutable2 = new MutableString(buffer, mutable1); CharSequence sub1 = mutable1.subSequence(4, 10); CharSequence sub2 = mutable2.subSequence(4, 10); assertThat("MutableSubString must equals", CharSequenceUtil.equals(sub, sub1)); assertThat("MutableSubString must equals", CharSequenceUtil.equals(sub, sub2)); }
@Test void toStringTest() { CharArray buffer = new CharArray(); String t1 = "TestString"; MutableString mutable1 = new MutableString(buffer, t1); MutableString mutable2 = new MutableString(buffer, mutable1); assertThat("MutableString must equals", t1.equals(mutable1.toString())); assertThat("MutableString must equals", t1.equals(mutable2.toString())); assertThat("MutableString must equals", mutable1.toString().equals(mutable2.toString())); assertThat("MutableString must equals", mutable1.toString().equals(t1)); assertThat("MutableString must equals", mutable2.toString().equals(t1)); assertThat("MutableString must equals", mutable1.toString().equals(mutable2.toString())); }
@Test void add() { CharArray buffer = new CharArray(); String t1 = "Test"; String t2 = "String"; String t3 = "Added"; MutableString m1 = new MutableString(buffer, t1); MutableString m3 = new MutableString(buffer, t3); MutableString m2 = new MutableString(buffer, t2); String shouldString = t1 + t2 + t3; m1.add(m2).add(m3); assertThat("Length must equals '" + shouldString.length() + "' : '" + m1.length() + "'", shouldString.length() == m1.length()); assertThat("MutableString must equals '" + shouldString + "' : '" + m1 + "'", CharSequenceUtil.equals(shouldString, m1)); assertThat("toString() must equals '" + shouldString + "' : '" + m1 + "'", shouldString.equals(m1.toString())); m1 = new MutableString(buffer, t1); m1.add(t2).add(m3); assertThat("Length must equals '" + shouldString.length() + "' : '" + m1.length() + "'", shouldString.length() == m1.length()); assertThat("MutableString must equals '" + shouldString + "' : '" + m1 + "'", CharSequenceUtil.equals(shouldString, m1)); assertThat("toString() must equals '" + shouldString + "' : '" + m1 + "'", shouldString.equals(m1.toString())); m1 = new MutableString(buffer, t1); m1.add(m2).add(m3).add(m2); String should2String = t1 + t2 + t3 + t2; assertThat("Length must equals '" + should2String.length() + "' : '" + m1.length() + "'", should2String.length() == m1.length()); assertThat("MutableString must equals '" + should2String + "' : '" + m1 + "'", CharSequenceUtil.equals(should2String, m1)); assertThat("toString() must equals '" + should2String + "' : '" + m1 + "'", should2String.equals(m1.toString())); CharSequence sub1 = should2String.subSequence(3, 13); CharSequence sub2 = m1.subSequence(3, 13); assertThat("SubSequences must equals '" + sub1 + "' : '" + sub2 + "'", CharSequenceUtil.equals(sub2, sub1)); }
public MutableString(CharArray storage, int ptr, int length) { this.storage = storage; this.ptr = ptr; this.length = length; }
public WordStore(boolean splitToWords) { this.storage = new CharArray(); this.asSingleWord = splitToWords; }
public ParameterStringSequence(String string) { this(new CharArray(), string); }
@Test void add() { String t1 = "Test"; StringBuilder sb = new StringBuilder(); sb.append("String"); MutableString mutable1 = new MutableString(new CharArray(), "Compound"); String shouldSequence = "CompoundTestString"; CompoundCharSequence sequence = new CompoundCharSequence(); sequence.add(mutable1, t1, sb); assertThat("Length must equals '" + shouldSequence.length() + "' : '" + sequence.length() + "'", shouldSequence.length() == sequence.length()); assertThat("Sequence must equals '" + shouldSequence + "' : '" + sequence + "'", CharSequenceUtil.equals(shouldSequence, sequence)); assertThat("toString() must equals '" + shouldSequence + "' : '" + sequence + "'", shouldSequence.equals(sequence.toString())); }