CommandMessenger.Escaping Class Reference

Utility class providing escaping functions More...

## Static Public Member Functions

static void | EscapeChars (char fieldSeparator, char commandSeparator, char escapeCharacter)
| Sets custom escape characters. More...
static string | Remove (string input, char removeChar, char escapeChar)
| Removes all occurences of a specific character unless escaped. More...
static String[] | Split (string input, char separator, char escapeCharacter, StringSplitOptions stringSplitOptions)
| Splits. More...
static string | Escape (string input)
| Escapes the input string. More...
static string | Unescape (string input)
| Unescapes the input string. More...

## Properties

static char | EscapeCharacter `[get]`
| Gets the escape character. More...

## Detailed Description

Utility class providing escaping functions

## Member Function Documentation

static string CommandMessenger.Escaping.Escape | ( | string | input | ) |

inlinestatic

Escapes the input string.

Parameters

  input | The unescaped input string.

Returns
  Escaped output string.

{
var escapeChars = new[]
{
_escapeCharacter.ToString(CultureInfo.InvariantCulture),
_fieldSeparator.ToString(CultureInfo.InvariantCulture),
_commandSeparator.ToString(CultureInfo.InvariantCulture),
"\0"
};
input = escapeChars.Aggregate(input,
(current, escapeChar) =>
current.Replace(escapeChar, _escapeCharacter + escapeChar));
return input;
}

static void CommandMessenger.Escaping.EscapeChars | ( | char | fieldSeparator,
|  | char | commandSeparator,
|  | char | escapeCharacter
| ) |  |

inlinestatic

Sets custom escape characters.

Parameters

  fieldSeparator | The field separator.
  commandSeparator | The command separator.
  escapeCharacter | The escape character.

{
_fieldSeparator = fieldSeparator;
_commandSeparator = commandSeparator;
_escapeCharacter = escapeCharacter;
}

static string CommandMessenger.Escaping.Remove | ( | string | input,
|  | char | removeChar,
|  | char | escapeChar
| ) |  |

inlinestatic

Removes all occurences of a specific character unless escaped.

Parameters

  input | The input.
  removeChar | The character to remove.
  escapeChar | The escape character.

Returns
  The string with all removeChars removed.

{
var output = "";
var escaped = new IsEscaped();
for (var i = 0; i < input.Length; i++)
{
char inputChar = input[i];
bool isEscaped = escaped.EscapedChar(inputChar);
if (inputChar != removeChar || isEscaped)
{
output += inputChar;
}
}
return output;
}

static String [] CommandMessenger.Escaping.Split | ( | string | input,
|  | char | separator,
|  | char | escapeCharacter,
|  | StringSplitOptions | stringSplitOptions
| ) |  |

inlinestatic

Splits.

Parameters

  input | The input.
  separator | The separator.
  escapeCharacter | The escape character.
  stringSplitOptions | Options for controlling the string split.

Returns
  The split string.

{
var word = "";
var result = new List<string>();
for (var i = 0; i < input.Length; i++)
{
var t = input[i];
if (t == separator)
{
result.Add(word);
word = "";
}
else
{
if (t == escapeCharacter)
{
word += t;
if (i < input.Length - 1) t = input[++i];
}
word += t;
}
}
result.Add(word);
if (stringSplitOptions == StringSplitOptions.RemoveEmptyEntries) result.RemoveAll(item => item == "");
return result.ToArray();
}

static string CommandMessenger.Escaping.Unescape | ( | string | input | ) |

inlinestatic

Unescapes the input string.

Parameters

  input | The escaped input string.

Returns
  The unescaped output string.

{
string output = "";
// Move unescaped characters right
for (var fromChar = 0; fromChar < input.Length; fromChar++)
{
if (input[fromChar] == _escapeCharacter)
{
fromChar++;
}
output += input[fromChar];
}
return output;
}

## Property Documentation

char CommandMessenger.Escaping.EscapeCharacter

staticget

Gets the escape character.

The escape character.

Referenced by CommandMessenger.IsEscaped.EscapedChar().

The documentation for this class was generated from the following file:

- D:/My Documents/Github/Arduino-Code-and-Libraries/Libraries/CmdMessenger/CSharp/CommandMessenger/Escaped.cs