### Eclipse Workspace Patch 1.0 #P groovy_1_6_X Index: src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java =================================================================== --- src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java (revision 14938) +++ src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java (working copy) @@ -9062,19 +9062,36 @@ * * @param self a String object * @return the denormalized string - * @throws java.io.IOException if an error occurs * @since 1.6.0 - * @see groovy.io.PlatformLineWriter */ - public static String denormalize(String self) throws IOException { - StringWriter result = new StringWriter(self.length()); - PlatformLineWriter w = new PlatformLineWriter(result, self.length()); - try { - w.write(self); - } finally { - closeWithWarning(w); + public static String denormalize(String self) { + final String sep = System.getProperty("line.separator"); + final int len = self.length(); + final StringBuilder sb = new StringBuilder(self.length()); + char chLookBack = '\0'; + + for (int i = 0; i < len; i++) { + final char ch = self.charAt(i); + + switch (ch) { + case '\r': + sb.append(sep); + break; + + case '\n': + if (chLookBack != '\r') + sb.append(sep); + break; + + default: + sb.append(ch); + break; + } + + chLookBack = ch; } - return result.toString(); + + return sb.toString(); } /** @@ -9086,25 +9103,26 @@ * @throws java.io.IOException if an error occurs * @since 1.6.0 */ - public static String normalize(String self) throws IOException { - // for efficiency, we don't use: return join(readLines(self), "\n"); - BufferedReader br = new BufferedReader(new StringReader(self)); - StringBuilder sb = new StringBuilder(self.length()); - boolean first = true; - try { - while (true) { - String line = br.readLine(); - if (line == null) { - break; - } else { - if (first) first = false; - else sb.append("\n"); - sb.append(line); - } + public static String normalize(String self) { + final int len = self.length(); + final StringBuilder sb = new StringBuilder(self.length()); + + for (int i = 0; i < len; i++) { + final char ch = self.charAt(i); + + if (ch == '\r') { + sb.append('\n'); + if (i + 1 < len && self.charAt(i + 1) == '\n') + i++; // skip the LF in CR LF + } else { + sb.append(ch); } - } finally { - closeWithWarning(br); } + + // why should we do this? + if (sb.length() > 0 && sb.charAt(sb.length() - 1) == '\n') + sb.setLength(sb.length() - 1); + return sb.toString(); }