freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ddek...@apache.org
Subject incubator-freemarker-docgen git commit: Fixed normalizeEOL method.
Date Fri, 01 Jan 2016 20:34:41 GMT
Repository: incubator-freemarker-docgen
Updated Branches:
  refs/heads/master d03201fa6 -> 99cfc0423


Fixed normalizeEOL method.


Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker-docgen/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker-docgen/commit/99cfc042
Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker-docgen/tree/99cfc042
Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker-docgen/diff/99cfc042

Branch: refs/heads/master
Commit: 99cfc042346a90c163d2d4d559e4d84e2c78213a
Parents: d03201f
Author: ddekany <ddekany@apache.org>
Authored: Fri Jan 1 21:34:32 2016 +0100
Committer: ddekany <ddekany@apache.org>
Committed: Fri Jan 1 21:34:32 2016 +0100

----------------------------------------------------------------------
 src/main/org/freemarker/docgen/TextUtil.java | 44 +++++++++++++++++++----
 1 file changed, 37 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker-docgen/blob/99cfc042/src/main/org/freemarker/docgen/TextUtil.java
----------------------------------------------------------------------
diff --git a/src/main/org/freemarker/docgen/TextUtil.java b/src/main/org/freemarker/docgen/TextUtil.java
index 5e6f8ae..d49f722 100644
--- a/src/main/org/freemarker/docgen/TextUtil.java
+++ b/src/main/org/freemarker/docgen/TextUtil.java
@@ -18,11 +18,6 @@
  */
 package org.freemarker.docgen;
 
-import java.io.FileNotFoundException;
-import java.io.IOException;
-
-import freemarker.template.utility.StringUtil;
-
 final class TextUtil {
 
     // Can't be instantiated
@@ -298,8 +293,43 @@ final class TextUtil {
         }
     }
     
-    public static String normalizeEOL(final String s, String eol) throws FileNotFoundException,
IOException {
-        return StringUtil.replace(s, "\r\n", eol).replace("\n", eol).replace("\r", eol);
+    public static String normalizeEOL(final String s, String eol) {
+        int ln = s.length();
+        int i = 0;
+        while (i < ln) {
+            char c = s.charAt(i);
+            if (c == '\r' || c == '\n') {
+                break;
+            }
+            i++;
+        }
+        if (i == ln) {
+            return s;
+        }
+
+        StringBuilder sb = new StringBuilder(ln + ln / 10);
+        int nextToAppend = 0;
+        while (i < ln) {
+            char c = s.charAt(i);
+            if (c == '\r' || c == '\n') {
+                sb.append(s, nextToAppend, i);
+                sb.append(eol);
+                if (c == '\r' && i + 1 < ln && s.charAt(i + 1) == '\n')
{
+                    i++;
+                }
+                nextToAppend = i + 1;
+            }
+            i++;
+        }
+        sb.append(s, nextToAppend, ln);
+        return sb.toString();
+    }
+    
+    public static void main(String[] args) {
+        System.out.println(normalizeEOL("", "|"));
+        System.out.println(normalizeEOL("abc", "|"));
+        System.out.println(normalizeEOL("a\nb\nc\r\nd", "|"));
+        System.out.println(normalizeEOL("\n\n\r\r\r\n", "|"));
     }
     
 }


Mime
View raw message