Skip to content

Commit

Permalink
DRY: grapheme length
Browse files Browse the repository at this point in the history
  • Loading branch information
radeusgd committed Mar 9, 2023
1 parent c5aba77 commit 563c8b1
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 15 deletions.
2 changes: 1 addition & 1 deletion std-bits/base/src/main/java/org/enso/base/Text_Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public static long char_length(String str) {
* @param str the string to measure
* @return length of the string
*/
private static long grapheme_length(String str) {
public static long grapheme_length(String str) {
return Core_Text_Utils.computeGraphemeLength(str);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.enso.table.aggregations;

import com.ibm.icu.text.BreakIterator;
import org.enso.base.Text_Utils;
import org.enso.table.data.column.storage.Storage;
import org.enso.table.data.table.Column;
import org.enso.table.data.table.problems.InvalidAggregation;
Expand All @@ -26,12 +27,12 @@ public Object aggregate(List<Integer> indexes) {
for (int row : indexes) {
Object value = storage.getItemBoxed(row);
if (value != null) {
if (!(value instanceof String)) {
if (!(value instanceof String asString)) {
this.addProblem(new InvalidAggregation(this.getName(), row, "Not a text value."));
return null;
}

long valueLength = GraphemeLength((String) value);
long valueLength = Text_Utils.grapheme_length(asString);
if (current == null || Long.compare(valueLength, length) == minOrMax) {
length = valueLength;
current = value;
Expand All @@ -41,16 +42,4 @@ public Object aggregate(List<Integer> indexes) {

return current;
}

private static long GraphemeLength(String text) {
BreakIterator iter = BreakIterator.getCharacterInstance();
iter.setText(text);

int count = 0;
for (int end = iter.next(); end != BreakIterator.DONE; end = iter.next()) {
count++;
}

return count;
}
}

0 comments on commit 563c8b1

Please sign in to comment.