diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2014-10-21 15:31:32 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2014-10-21 15:31:32 +0200 |
commit | 15cea0177b7a94eb2eb9b9687e04da9e3832bef4 (patch) | |
tree | b5646ed332204301e6ffe767339a4b87d3ecfc58 | |
parent | 20f2ddd5f6033d74c9a9de7b865ee82c62ee7c72 (diff) | |
parent | 7c3da87ee6fe5cc4b26d049c0bb1e38e884d1b0b (diff) | |
download | sonarqube-15cea0177b7a94eb2eb9b9687e04da9e3832bef4.tar.gz sonarqube-15cea0177b7a94eb2eb9b9687e04da9e3832bef4.zip |
Merge branch 'reduce-complexity-keyvalueformat' into branch-4.5
-rw-r--r-- | sonar-plugin-api/src/main/java/org/sonar/api/utils/KeyValueFormat.java | 75 |
1 files changed, 45 insertions, 30 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/KeyValueFormat.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/KeyValueFormat.java index 37c16c9c8f7..a9914eee1ed 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/KeyValueFormat.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/KeyValueFormat.java @@ -90,46 +90,61 @@ public final class KeyValueFormat { return next(';'); } + private class Context { + private StringBuilder result = new StringBuilder(); + private boolean escaped = false; + private char firstChar; + private char previous = (char) -1; + } + @CheckForNull private String next(char separator) { if (position >= csv.length()) { return null; } - StringBuilder result = new StringBuilder(); - boolean escaped = false; - char firstChar = csv.charAt(position); - char previous = (char) -1; + Context context = new Context(); + context.firstChar = csv.charAt(position); // check if value is escaped by analyzing first character - if (firstChar == DOUBLE_QUOTE) { - escaped = true; - position++; - previous = firstChar; + checkEscaped(context); + + boolean isEnd = false; + while (position < csv.length() && !isEnd) { + isEnd = advance(separator, context); } + return context.result.toString(); + } + private boolean advance(char separator, Context context) { boolean end = false; - while (position < csv.length() && !end) { - char c = csv.charAt(position); - if (c == separator && !escaped) { - end = true; - position++; - } else if (c == '\\' && escaped && position < csv.length() + 1 && csv.charAt(position + 1) == DOUBLE_QUOTE) { - // on a backslash that escapes double-quotes -> keep double-quotes and jump after - previous = DOUBLE_QUOTE; - result.append(previous); - position += 2; - } else if (c == '"' && escaped && previous != '\\') { - // on unescaped double-quotes -> end of escaping. - // assume that next character is a separator (= or ;). This could be - // improved to enforce check. - end = true; - position += 2; - } else { - result.append(c); - previous = c; - position++; - } + char c = csv.charAt(position); + if (c == separator && !context.escaped) { + end = true; + position++; + } else if (c == '\\' && context.escaped && position < csv.length() + 1 && csv.charAt(position + 1) == DOUBLE_QUOTE) { + // on a backslash that escapes double-quotes -> keep double-quotes and jump after + context.previous = DOUBLE_QUOTE; + context.result.append(context.previous); + position += 2; + } else if (c == '"' && context.escaped && context.previous != '\\') { + // on unescaped double-quotes -> end of escaping. + // assume that next character is a separator (= or ;). This could be + // improved to enforce check. + end = true; + position += 2; + } else { + context.result.append(c); + context.previous = c; + position++; + } + return end; + } + + private void checkEscaped(Context context) { + if (context.firstChar == DOUBLE_QUOTE) { + context.escaped = true; + position++; + context.previous = context.firstChar; } - return result.toString(); } } |