FormatterImpl.h
Go to the documentation of this file.
1 #pragma once
2 
3 template<class T>
5 {
6 public:
7 
8  // \todo add flags to change behavior of mapping variables that don't exist, currently they are left untouched
9  static int FormatTemplate(std::basic_string<T>& s, const std::map<std::basic_string<T>, std::basic_string<T>>& variables, const std::basic_string<T>& left, const std::basic_string<T>& right)
10  {
11  if (left.empty() || right.empty())
12  {
13  throw std::exception("Missing l/r delimiter(s)");
14  }
15 
16  if (s.empty())
17  {
18  return 0;
19  }
20 
21  int ii = 0;
22  int result = 0;
23 
24  while ((ii = s.find(left, ii)) != s.npos)
25  {
26  int jj = s.find(right, ii + left.length());
27  if (jj == s.npos)
28  break;
29 
30  int replace_length = jj - ii + right.length();
31  int variable_length = jj - ii - left.length();
32 
33  std::basic_string<T> variable = s.substr(ii + left.length(), variable_length);
34  std::map<std::basic_string<T>, std::basic_string<T>>::const_iterator iter = variables.find(variable);
35  if (iter != variables.end())
36  {
37  std::basic_string<T> variable_value = iter->second;
38  s.replace(ii, replace_length, variable_value);
39  ii += variable_value.length();
40  }
41  else
42  {
43  ii += replace_length;
44  }
45 
46  result ++;
47  }
48 
49  return result;
50  }
51 };
static int FormatTemplate(std::basic_string< T > &s, const std::map< std::basic_string< T >, std::basic_string< T >> &variables, const std::basic_string< T > &left, const std::basic_string< T > &right)
Definition: FormatterImpl.h:9
Definition: FormatterImpl.h:4


© Application Security Inc. - All Rights Reserved http://msiext.codeplex.com