Empeld
Empeld plugin documentation.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events
pluginbase.Helpers.Data.TokenizedString Class Reference

Fast implementation of a string tokenizer. Tokens exist like {token} More...

Inheritance diagram for pluginbase.Helpers.Data.TokenizedString:

Public Member Functions

 TokenizedString (string str)
 Create a tokenized string from a normal string More...
 
bool SetToken (string token, string val)
 Sets a token in the instance of a string to a value More...
 
bool SetToken (string token, object val)
 Sets a token in the instance of the string. More...
 
bool SetToken (string token, string format, params object[] args)
 Helper: Sets a token to the formatted string More...
 
int SetTokens (IDictionary< string, object > vals)
 Sets tokens given a dictionary of items More...
 
void ClearTokens ()
 Reset all tokens to be empty string More...
 

Static Public Member Functions

static string Render (string str, IDictionary< string, object > tokens)
 Helper method to render a new string, given one with tokens, and a mapping dictionary More...
 
static string Render (string str, object model)
 Render helper to create a string from a tokenized one and an anonymous model More...
 

Public Attributes

readonly string OriginalString
 The original string with tokens intact More...
 

Properties

int Segments [get]
 The total number of token segments More...
 
int TokenCount [get]
 The total number of replaceable tokens More...
 
IEnumerable< string > Tokens [get]
 An enumerable representing the token names More...
 

Detailed Description

Fast implementation of a string tokenizer. Tokens exist like {token}

Constructor & Destructor Documentation

pluginbase.Helpers.Data.TokenizedString.TokenizedString ( string  str)

Create a tokenized string from a normal string

Parameters
strString.

Attribute: i

;

Attribute: i + 1

: (char)0;

Attribute: parts[0]

= new Token

Attribute: 1

: null

64  {
65  if (str == null)
66  throw new ArgumentNullException("str");
67 
68  this.OriginalString = str;
69  this._builder.Capacity = this.OriginalString.Length;
70 
71  var tokens = new List<string>();
72  var sb = new StringBuilder(this.OriginalString.Length);
73 
74  bool isToken = false;
75  for (int i = 0; i<str.Length; ++i)
76  {
77  char c = str[i];
78  char cNext = i < str.Length - 1 ? str[i + 1] : (char)0;
79 
80  if (c == T_OPEN && cNext == T_OPEN)
81  {
82  sb.Append(c);
83  ++i;
84  }
85  else if(c == T_OPEN)
86  {
87  if (isToken)
88  throw new FormatException("Token already open. Can't have a token in a token");
89 
90  isToken = true;
91  if (sb.Length > 0)
92  tokens.Add(sb.ToString());
93  sb.Clear();
94  }
95  else if (c == T_CLOSE && cNext == T_CLOSE)
96  {
97  sb.Append(c);
98  ++i;
99  }
100  else if (c == T_CLOSE)
101  {
102  if (!isToken)
103  throw new FormatException("Token was closed, but never opened");
104  if (sb.Length == 0)
105  throw new FormatException("Empty token not valid");
106 
107  isToken = false;
108  tokens.Add(string.Empty);
109 
110  string[] parts = sb.ToString().Split(':');
111  _tokenPositions[parts[0]] = new Token
112  {
113  Pos = tokens.Count - 1,
114  Format = parts.Length >= 2 ? parts[1] : null
115  };
116  sb.Clear();
117  }
118  else
119  {
120  sb.Append(c);
121  }
122  }
123 
124  if (isToken)
125  throw new FormatException("Token open but was never closed");
126 
127  //End
128  if (sb.Length > 0)
129  tokens.Add(sb.ToString());
130 
131  _tokens = tokens.ToArray();
132  }
readonly string OriginalString
The original string with tokens intact
Definition: TokenizedString.cs:30

Member Function Documentation

void pluginbase.Helpers.Data.TokenizedString.ClearTokens ( )

Reset all tokens to be empty string

Attribute: item.Pos

= string.Empty;

239  {
240  foreach(var item in _tokenPositions.Values)
241  {
242  _tokens[item.Pos] = string.Empty;
243  }
244  }
static string pluginbase.Helpers.Data.TokenizedString.Render ( string  str,
IDictionary< string, object >  tokens 
)
static

Helper method to render a new string, given one with tokens, and a mapping dictionary

Parameters
strString.
tokensTokens.
148  {
149  var s = new TokenizedString(str);
150  s.SetTokens(tokens);
151  return s.ToString();
152  }
TokenizedString(string str)
Create a tokenized string from a normal string
Definition: TokenizedString.cs:63
static string pluginbase.Helpers.Data.TokenizedString.Render ( string  str,
object  model 
)
static

Render helper to create a string from a tokenized one and an anonymous model

Parameters
strString.
modelModel.

Attribute: prop.Name

= prop.GetValue(model, null);

160  {
161  var dict = new Dictionary<string, object>();
162  foreach(var prop in model.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
163  {
164  dict[prop.Name] = prop.GetValue(model, null);
165  }
166  return Render(str, dict);
167  }
static string Render(string str, IDictionary< string, object > tokens)
Helper method to render a new string, given one with tokens, and a mapping dictionary ...
Definition: TokenizedString.cs:147
bool pluginbase.Helpers.Data.TokenizedString.SetToken ( string  token,
string  val 
)

Sets a token in the instance of a string to a value

Returns
true, if token was set, false otherwise.
Parameters
tokenToken.
valValue.

Attribute: tok.Pos

= val;

176  {
177  Token tok;
178  if (_tokenPositions.TryGetValue(token, out tok))
179  {
180  _tokens[tok.Pos] = val;
181  return true;
182  }
183  return false;
184  }
bool pluginbase.Helpers.Data.TokenizedString.SetToken ( string  token,
object  val 
)

Sets a token in the instance of the string.

Returns
true, if token was set, false otherwise.
Parameters
tokenToken.
valValue.

Attribute: tok.Pos

= formattable.ToString(tok.Format, null);

Attribute: tok.Pos

= val.ToString();

193  {
194  Token tok;
195  if (_tokenPositions.TryGetValue(token, out tok))
196  {
197  IFormattable formattable;
198  if (tok.Format != null && (formattable = val as IFormattable) != null)
199  _tokens[tok.Pos] = formattable.ToString(tok.Format, null);
200  else
201  _tokens[tok.Pos] = val.ToString();
202  return true;
203  }
204  return false;
205  }
bool pluginbase.Helpers.Data.TokenizedString.SetToken ( string  token,
string  format,
params object[]  args 
)

Helper: Sets a token to the formatted string

Returns
true, if token was set, false otherwise.
Parameters
tokenToken.
formatFormat.
argsArguments.
215  {
216  return this.SetToken(token, string.Format(format, args));
217  }
bool SetToken(string token, string val)
Sets a token in the instance of a string to a value
Definition: TokenizedString.cs:175
int pluginbase.Helpers.Data.TokenizedString.SetTokens ( IDictionary< string, object >  vals)

Sets tokens given a dictionary of items

Returns
The tokens.
Parameters
valsVals.
225  {
226  int count = 0;
227  foreach(var item in vals)
228  {
229  if (this.SetToken(item.Key, item.Value))
230  count++;
231  }
232  return count;
233  }
bool SetToken(string token, string val)
Sets a token in the instance of a string to a value
Definition: TokenizedString.cs:175

Member Data Documentation

readonly string pluginbase.Helpers.Data.TokenizedString.OriginalString

The original string with tokens intact

Property Documentation

int pluginbase.Helpers.Data.TokenizedString.Segments
get

The total number of token segments

The segments.

int pluginbase.Helpers.Data.TokenizedString.TokenCount
get

The total number of replaceable tokens

The token count.

IEnumerable<string> pluginbase.Helpers.Data.TokenizedString.Tokens
get

An enumerable representing the token names

The tokens.


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