split a String into lines
Signed-off-by: Florian Thienel <florian@thienel.org>
diff --git a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/core/TextUtilsTest.java b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/core/TextUtilsTest.java
new file mode 100644
index 0000000..ddca0d1
--- /dev/null
+++ b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/core/TextUtilsTest.java
@@ -0,0 +1,50 @@
+/*******************************************************************************
+ * Copyright (c) 2015 Florian Thienel and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Florian Thienel - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.vex.core.internal.core;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Arrays;
+
+import org.junit.Test;
+
+public class TextUtilsTest {
+
+ @Test
+ public void provideLinesDelimitedWithLF() throws Exception {
+ assertEquals(Arrays.asList("line 1", "line 2", "line 3"), Arrays.asList(TextUtils.lines("line 1\nline 2\nline 3")));
+ }
+
+ @Test
+ public void provideLinesDelimitedWithCR() throws Exception {
+ assertEquals(Arrays.asList("line 1", "line 2", "line 3"), Arrays.asList(TextUtils.lines("line 1\rline 2\rline 3")));
+ }
+
+ @Test
+ public void provideLinesDelimitedWithCRLF() throws Exception {
+ assertEquals(Arrays.asList("line 1", "line 2", "line 3"), Arrays.asList(TextUtils.lines("line 1\r\nline 2\r\nline 3")));
+ }
+
+ @Test
+ public void provideLinesDelimitedWithDifferentLineSeparators() throws Exception {
+ assertEquals(Arrays.asList("line 1", "line 2", "line 3", "line 4", "line 5"), Arrays.asList(TextUtils.lines("line 1\rline 2\nline 3\r\nline 4\rline 5")));
+ }
+
+ @Test
+ public void provideEmptyLines() throws Exception {
+ assertEquals(Arrays.asList("", "line 1", "", "line 2", "line 3", ""), Arrays.asList(TextUtils.lines("\nline 1\n\nline 2\nline 3\n")));
+ }
+
+ @Test
+ public void provideSingleLine() throws Exception {
+ assertEquals(Arrays.asList("line 1"), Arrays.asList(TextUtils.lines("line 1")));
+ }
+}
diff --git a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/core/TextUtils.java b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/core/TextUtils.java
index efa137b..0a4f4a1 100644
--- a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/core/TextUtils.java
+++ b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/core/TextUtils.java
@@ -10,8 +10,12 @@
*******************************************************************************/
package org.eclipse.vex.core.internal.core;
+import java.util.regex.Pattern;
+
public class TextUtils {
+ private static final Pattern ANY_LINE_BREAKS = Pattern.compile("(\r\n|\r|\n)");
+
public static int countWhitespaceAtStart(final String text) {
int whitespaceCount = 0;
while (whitespaceCount < text.length()) {
@@ -37,4 +41,8 @@
return whitespaceCount;
}
+ public static String[] lines(final String s) {
+ return ANY_LINE_BREAKS.split(s, -1);
+ }
+
}