コード例 #1
0
        internal static string EscapeSpecialCharacters(string unescapedValue)
        {
            if (string.IsNullOrEmpty(unescapedValue))
            {
                return(string.Empty);
            }

            var original = new StringBuffer(unescapedValue);
            var escaped  = new StringBuffer();

            if (original.Contains(' '))
            {
                escaped.PushToEnd(SpChar_LiteralStringBlock);
                escaped.TransferToEnd(original);
                escaped.PushToEnd(SpChar_LiteralStringBlock);
            }
            else
            {
                while (!original.IsEmpty)
                {
                    switch (original.PeekStart())
                    {
                    case SpChar_KeyDelimiter:
                    case SpChar_ValueDelimiter:
                    case SpChar_BeginComplexValue:
                    case SpChar_FinishComplexValue:
                    case SpChar_ListItemSplitter:
                    case SpChar_CommentBlock:
                    case SpChar_EscapeChar:
                        escaped.PushToEnd(SpChar_EscapeChar);
                        break;

                    default:
                        break;
                    }

                    escaped.PushToEnd(original.PopFromStart());
                }
            }

            return(escaped.ToString());
        }