diff options
author | James Moger <james.moger@gitblit.com> | 2012-09-10 16:25:10 -0400 |
---|---|---|
committer | James Moger <james.moger@gitblit.com> | 2012-09-10 16:25:10 -0400 |
commit | b34048803ad6cf0a0a0c998696a41de118715452 (patch) | |
tree | 35aecba4e377b6fad18382af1ea2d7d08cd6045e /src/com/gitblit/utils/StringUtils.java | |
parent | 8d6217d27bcd171d1d8276360e261bcaf8df9272 (diff) | |
download | gitblit-b34048803ad6cf0a0a0c998696a41de118715452.tar.gz gitblit-b34048803ad6cf0a0a0c998696a41de118715452.zip |
Fix bug in diff view for filenames with non-ASCII characters (issue 128)
Diffstat (limited to 'src/com/gitblit/utils/StringUtils.java')
-rw-r--r-- | src/com/gitblit/utils/StringUtils.java | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/com/gitblit/utils/StringUtils.java b/src/com/gitblit/utils/StringUtils.java index 08fd4972..e4407901 100644 --- a/src/com/gitblit/utils/StringUtils.java +++ b/src/com/gitblit/utils/StringUtils.java @@ -15,6 +15,7 @@ */
package com.gitblit.utils;
+import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
@@ -624,4 +625,39 @@ public class StringUtils { }
return url;
}
+
+ /**
+ * Converts a string with \nnn sequences into a UTF-8 encoded string.
+ * @param input
+ * @return
+ */
+ public static String convertOctal(String input) {
+ try {
+ ByteArrayOutputStream bytes = new ByteArrayOutputStream();
+ Pattern p = Pattern.compile("(\\\\\\d{3})");
+ Matcher m = p.matcher(input);
+ int i = 0;
+ while (m.find()) {
+ bytes.write(input.substring(i, m.start()).getBytes("UTF-8"));
+ // replace octal encoded value
+ // strip leading \ character
+ String oct = m.group().substring(1);
+ bytes.write(Integer.parseInt(oct, 8));
+ i = m.end();
+ }
+ if (bytes.size() == 0) {
+ // no octal matches
+ return input;
+ } else {
+ if (i < input.length()) {
+ // add remainder of string
+ bytes.write(input.substring(i).getBytes("UTF-8"));
+ }
+ }
+ return bytes.toString("UTF-8");
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return input;
+ }
}
\ No newline at end of file |