Sorts the selected text. @return true if text has changed
()
| 658 | * @return {@code true} if text has changed |
| 659 | */ |
| 660 | boolean sort() { |
| 661 | if(!isSelected()) selectAll(); |
| 662 | if(!extend()) return false; |
| 663 | |
| 664 | // count lines |
| 665 | int l = 1; |
| 666 | final int s = start, e = end, ts = size(); |
| 667 | final byte[] tmp = Arrays.copyOf(text, ts); |
| 668 | for(int i = s; i < e; i++) { |
| 669 | if(tmp[i] == '\n') l++; |
| 670 | } |
| 671 | |
| 672 | // collect lines to be sorted |
| 673 | final TokenList tl = new TokenList(l); |
| 674 | final ByteList bl = new ByteList(); |
| 675 | for(int i = s; i < e; i++) { |
| 676 | final byte ch = tmp[i]; |
| 677 | if(ch == '\n') { |
| 678 | tl.add(bl.next()); |
| 679 | } else { |
| 680 | bl.add(ch); |
| 681 | } |
| 682 | } |
| 683 | if(!bl.isEmpty()) tl.add(bl.finish()); |
| 684 | sort(tl); |
| 685 | |
| 686 | // copy lines back to text |
| 687 | int i = s; |
| 688 | for(final byte[] line : tl) { |
| 689 | final int ll = line.length; |
| 690 | Array.copyFromStart(line, ll, tmp, i); |
| 691 | i += ll; |
| 692 | if(i < e) tmp[i++] = '\n'; |
| 693 | } |
| 694 | if(i < e) Array.copy(tmp, e, ts - e, tmp, i); |
| 695 | final boolean changed = text(i == e ? tmp : Arrays.copyOf(tmp, ts - e + i)); |
| 696 | select(s, i); |
| 697 | return changed; |
| 698 | } |
| 699 | |
| 700 | /** |
| 701 | * Sorts the specified data. |
nothing calls this directly
no test coverage detected