CmdMessenger  3.0
CmdMessenger is a serial port messaging library for the Arduino
Static Public Member Functions | Properties | List of all members
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
inputThe unescaped input string.
Returns
Escaped output string.
144  {
145  var escapeChars = new[]
146  {
147  _escapeCharacter.ToString(CultureInfo.InvariantCulture),
148  _fieldSeparator.ToString(CultureInfo.InvariantCulture),
149  _commandSeparator.ToString(CultureInfo.InvariantCulture),
150  "\0"
151  };
152  input = escapeChars.Aggregate(input,
153  (current, escapeChar) =>
154  current.Replace(escapeChar, _escapeCharacter + escapeChar));
155  return input;
156  }
static void CommandMessenger.Escaping.EscapeChars ( char  fieldSeparator,
char  commandSeparator,
char  escapeCharacter 
)
inlinestatic

Sets custom escape characters.

Parameters
fieldSeparatorThe field separator.
commandSeparatorThe command separator.
escapeCharacterThe escape character.
77  {
78  _fieldSeparator = fieldSeparator;
79  _commandSeparator = commandSeparator;
80  _escapeCharacter = escapeCharacter;
81  }
static string CommandMessenger.Escaping.Remove ( string  input,
char  removeChar,
char  escapeChar 
)
inlinestatic

Removes all occurences of a specific character unless escaped.

Parameters
inputThe input.
removeCharThe character to remove.
escapeCharThe escape character.
Returns
The string with all removeChars removed.
89  {
90  var output = "";
91  var escaped = new IsEscaped();
92  for (var i = 0; i < input.Length; i++)
93  {
94  char inputChar = input[i];
95  bool isEscaped = escaped.EscapedChar(inputChar);
96  if (inputChar != removeChar || isEscaped)
97  {
98  output += inputChar;
99  }
100  }
101  return output;
102  }
static String [] CommandMessenger.Escaping.Split ( string  input,
char  separator,
char  escapeCharacter,
StringSplitOptions  stringSplitOptions 
)
inlinestatic

Splits.

Parameters
inputThe input.
separatorThe separator.
escapeCharacterThe escape character.
stringSplitOptionsOptions for controlling the string split.
Returns
The split string.
114  {
115  var word = "";
116  var result = new List<string>();
117  for (var i = 0; i < input.Length; i++)
118  {
119  var t = input[i];
120  if (t == separator)
121  {
122  result.Add(word);
123  word = "";
124  }
125  else
126  {
127  if (t == escapeCharacter)
128  {
129  word += t;
130  if (i < input.Length - 1) t = input[++i];
131  }
132  word += t;
133  }
134  }
135  result.Add(word);
136  if (stringSplitOptions == StringSplitOptions.RemoveEmptyEntries) result.RemoveAll(item => item == "");
137  return result.ToArray();
138  }
static string CommandMessenger.Escaping.Unescape ( string  input)
inlinestatic

Unescapes the input string.

Parameters
inputThe escaped input string.
Returns
The unescaped output string.
162  {
163  string output = "";
164  // Move unescaped characters right
165  for (var fromChar = 0; fromChar < input.Length; fromChar++)
166  {
167  if (input[fromChar] == _escapeCharacter)
168  {
169  fromChar++;
170  }
171  output += input[fromChar];
172  }
173  return output;
174  }

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: