public static int stringWidth(String str) { int total = 0; for (int i = 0; i < str.length(); ) { int cp = str.codePointAt(i); int width = codePointWidth(cp); // System.out.printf("Code point %c width: %d\n", cp, width); total += width; i += Character.charCount(cp); } return total; }
public void setTags(String[] tags) { if (tags == null) { throw new IllegalArgumentException("Tags passed to setTags cannot be null"); } this.tags = new ArrayList<>(); /* Only add non-null and non-empty strings. */ for (String tag: tags) { if (!OrgStringUtils.isEmpty(tag)) { this.tags.add(tag); } } }
public String toString() { return OrgStringUtils.join(this, DELIMITER); } }
@Test public void testCharacterWidth() { String str = "A"; Assert.assertEquals(1, OrgStringUtils.stringWidth(str)); }
/** * Make sure content ends with at least 2 new-line characters, unless it's empty. * Required so notes can be just appended to it. * * @param content Preface * @return preface ready for appending */ public String whiteSpacedFilePreface(String content) { if (content == null || content.length() == 0) { return ""; } else { return OrgStringUtils.trimLines(content) + "\n\n"; } }
@Test public void testSurrogatePairWidth() { String str = "𥑮"; Assert.assertEquals(2, OrgStringUtils.stringWidth(str)); }
/** * Removes empty lines before and after the content. * Called before every announcement. */ private void setTrimmedPreface(OrgFile file, String preface) { file.setPreface(OrgStringUtils.trimLines(preface)); } }
@Test public void testWideWidth() { String str = "漢"; Assert.assertEquals(2, OrgStringUtils.stringWidth(str)); } @Test
public static OrgDateTime parseOrNull(String str) { if (OrgStringUtils.isEmpty(str)) { return null; } OrgDateTime time = new OrgDateTime(); time.string = str; return time; }
/** * Removes empty lines before and after the content. * Called before every announcement. */ private void trimContent(OrgHead head) { head.setContent(OrgStringUtils.trimLines(head.getContent())); }
/** * Given a sets of to do and done keywords, builds a regex for matching against heading. * * @return {@link Pattern} or null if no valid keywords were found. */ private Pattern buildStatePattern(Set<String> todoKeywords, Set<String> doneKeywords) { Set<String> allKeywords = new HashSet<>(); if (todoKeywords != null) { for (String keyword: todoKeywords) { allKeywords.add(Pattern.quote(keyword)); } } if (doneKeywords != null) { for (String keyword: doneKeywords) { allKeywords.add(Pattern.quote(keyword)); } } if (allKeywords.size() > 0) { return Pattern.compile("^(" + OrgStringUtils.join(allKeywords, "|") + ")(.*)"); } return null; }
@Test public void testStringWidth() { String str = "A漢字𥑮"; Assert.assertEquals(7, OrgStringUtils.stringWidth(str)); } }
public static OrgRange parseOrNull(String str) { if (OrgStringUtils.isEmpty(str)) { return null; } return parse(str); }
int padding = Math.abs(settings.tagsColumn) - OrgStringUtils.stringWidth(s.toString()); padding -= OrgStringUtils.stringWidth(ts);
private void parseTimeOfDay(String str) { Matcher m = OrgPatterns.TIME_OF_DAY_P.matcher(str); if (! m.find()) { matchFailed(str, OrgPatterns.TIME_OF_DAY_P); } cal.set(Calendar.HOUR_OF_DAY, Integer.valueOf(m.group(2))); cal.set(Calendar.MINUTE, Integer.valueOf(m.group(3))); hasTime = true; if (! OrgStringUtils.isEmpty(m.group(4))) { // End time exists endCal = Calendar.getInstance(); endCal.setTime(cal.getTime()); endCal.set(Calendar.HOUR_OF_DAY, Integer.valueOf(m.group(6))); endCal.set(Calendar.MINUTE, Integer.valueOf(m.group(7))); endCal.set(Calendar.SECOND, 0); endCal.set(Calendar.MILLISECOND, 0); } }
cal.set(Calendar.DAY_OF_MONTH, Integer.valueOf(m.group(4))); if (! OrgStringUtils.isEmpty(m.group(6))) { // Has time of day. parseTimeOfDay(string.substring(m.start(6))); } else {
public static OrgRepeater parse(String str) { OrgRepeater repeater = new OrgRepeater(); Matcher m = OrgPatterns.REPEATER.matcher(str); if (m.find()) { if (m.groupCount() == 7) { repeater.setTypeFromString(m.group(2)); repeater.setValue(m.group(3)); repeater.setUnit(m.group(4)); if (! OrgStringUtils.isEmpty(m.group(6))) { repeater.habitDeadline = new OrgInterval(); repeater.habitDeadline.setValue(m.group(6)); repeater.habitDeadline.setUnit(m.group(7)); } } else { throw new IllegalArgumentException("Expected 7 groups (got " + m.groupCount() + ") when matching repeater " + str + " against " + OrgPatterns.REPEATER); } } else { throw new IllegalArgumentException("Failed matching repeater " + str + " against " + OrgPatterns.REPEATER); } return repeater; }