#include "Lsys.hpp"

namespace Lsystem
{
  string iterate(string tree)
  {
    size_t curly_left = tree.find('{');
    size_t curly_right = tree.rfind('}');
    size_t i = curly_right + 1;
    while (i < tree.length())
    {
      size_t key = curly_left + 1;  //start of the rules, the key
      //for every rules
      while (key < curly_right)
      {
        size_t colon = key + 1;                     //the value, while the key is one char len
        size_t semicolon = tree.find(";",colon);    //end of the value
        if (tree.at(i) == tree.at(key))
        {
          size_t length = semicolon-colon - 1;      //length of the value
          tree.replace(i, 1, tree.substr(colon+1, length));
          key = curly_right;
          i = i + length - 1;                       //minus one because there is a +1 below
        }
        else
        {
          key = semicolon + 1;
        }
      }
      i++;
    }
    return tree;
  }
} 
