Skip to content

Commit

Permalink
Some updates
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfsck committed Feb 29, 2016
1 parent 8cde081 commit 8e089cf
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 13 deletions.
16 changes: 11 additions & 5 deletions ICSharpCode.NRefactory.CSharp/CommentReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ You should have received a copy of the GNU General Public License

namespace ICSharpCode.NRefactory.CSharp {
public struct CommentReference {
public int Length;
public object Reference;
public bool IsLocal;
public readonly int Length;
public readonly object Reference;
public readonly bool IsLocal;

public CommentReference(int len, object @ref, bool isLocal = false) {
this.Length = len;
Expand All @@ -34,8 +34,8 @@ public CommentReference(int len, object @ref, bool isLocal = false) {
}

public sealed class CommentReferencesCreator {
readonly List<CommentReference> refs = new List<CommentReference>();
readonly StringBuilder sb = new StringBuilder();
readonly List<CommentReference> refs;
readonly StringBuilder sb;

public CommentReference[] CommentReferences {
get { return refs.ToArray(); }
Expand All @@ -45,6 +45,12 @@ public string Text {
get { return sb.ToString(); }
}

public CommentReferencesCreator(StringBuilder sb) {
this.refs = new List<CommentReference>();
this.sb = sb;
this.sb.Clear();
}

public void AddText(string text) {
Add(text, null, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public static string PrintPrimitiveValue(object value)

public override void WritePrimitiveValue(object value, TextTokenKind? tokenKind = null, string literalValue = null)
{
WritePrimitiveValue(value, tokenKind, literalValue, ref column, (a, b) => textWriter.Write(a), (a, b, c) => WriteToken(a, b, c));
WritePrimitiveValue(value, tokenKind, literalValue, ref column, (a, b) => textWriter.Write(a), WriteToken);
}

public static void WritePrimitiveValue(object value, TextTokenKind? tokenKind, string literalValue, ref int column, Action<string, TextTokenKind> writer, Action<Role, string, TextTokenKind> writeToken)
Expand Down Expand Up @@ -242,9 +242,10 @@ public static void WritePrimitiveValue(object value, TextTokenKind? tokenKind, s
}
return;
}

if (value is string) {
string tmp = "\"" + ConvertString(value.ToString()) + "\"";

var s = value as string;
if (s != null) {
string tmp = "\"" + ConvertString(s) + "\"";
column += tmp.Length;
writer(tmp, TextTokenKind.String);
} else if (value is char) {
Expand Down Expand Up @@ -333,7 +334,7 @@ public static void WritePrimitiveValue(object value, TextTokenKind? tokenKind, s
writer(b.ToString(), TextTokenKind.Number);
column += b.Length;
} else {
var s = value.ToString();
s = value.ToString();
writer(s, TextTokenKindUtils.GetTextTokenType(value));
column += s.Length;
}
Expand Down Expand Up @@ -386,18 +387,89 @@ static string ConvertChar(char ch)
}
}
}

static void AppendChar(StringBuilder sb, char ch)
{
switch (ch) {
case '\\':
sb.Append("\\\\");
break;
case '\0':
sb.Append("\\0");
break;
case '\a':
sb.Append("\\a");
break;
case '\b':
sb.Append("\\b");
break;
case '\f':
sb.Append("\\f");
break;
case '\n':
sb.Append("\\n");
break;
case '\r':
sb.Append("\\r");
break;
case '\t':
sb.Append("\\t");
break;
case '\v':
sb.Append("\\v");
break;
default:
if (char.IsControl(ch) || char.IsSurrogate(ch) ||
// print all uncommon white spaces as numbers
(char.IsWhiteSpace(ch) && ch != ' ')) {
sb.Append("\\u");
sb.Append(((int)ch).ToString("x4"));
} else {
sb.Append(ch);
}
break;
}
}

/// <summary>
/// Converts special characters to escape sequences within the given string.
/// </summary>
public static string ConvertString(string str)
{
StringBuilder sb = new StringBuilder ();
foreach (char ch in str) {
int i = 0;
for (; ; i++) {
if (i >= str.Length)
return str;
char c = str[i];
switch (c) {
case '"':
case '\\':
case '\0':
case '\a':
case '\b':
case '\f':
case '\n':
case '\r':
case '\t':
case '\v':
goto escapeChars;
default:
if (char.IsControl(c) || char.IsSurrogate(c) || (char.IsWhiteSpace(c) && c != ' '))
goto escapeChars;
break;
}
}

escapeChars:
StringBuilder sb = new StringBuilder();
if (i > 0)
sb.Append(str, 0, i);
for (; i < str.Length; i++) {
char ch = str[i];
if (ch == '"') {
sb.Append("\\\"");
} else {
sb.Append(ConvertChar(ch));
AppendChar(sb, ch);
}
}
return sb.ToString();
Expand Down

0 comments on commit 8e089cf

Please sign in to comment.