Skip to content

File parenthesisstripper.h

File List > code_source > templat > include > TempLat > util > parenthesisstripper.h

Go to the documentation of this file

#ifndef TEMPLAT_UTIL_PARENTHESISSTRIPPER_H
#define TEMPLAT_UTIL_PARENTHESISSTRIPPER_H

/* This file is part of TempLat, available at https://cosmolattice.github.io/templat .
   Copyright 2021-2026 The TempLat authors, see AUTHORS.md.
   Released under the MIT license, see LICENSE.md. */

// File info: Main contributor(s): Adrien Florio, Year: 2020

#include "TempLat/util/exception.h"

namespace TempLat
{
  class ParenthesisStripper
  {
  public:
    // Put public methods here. These should change very little over time.
    ParenthesisStripper() = delete;

    static void strip(std::string &toStrip)
    {
      if ((toStrip.front() == '(' && findClosing(toStrip, '(', ')') == toStrip.size() - 1) or
          (toStrip.front() == '[' && findClosing(toStrip, '[', ']') == toStrip.size() - 1) or
          (toStrip.front() == '{' && findClosing(toStrip, '{', '}') == toStrip.size() - 1))
        toStrip = toStrip.substr(1, toStrip.size() - 2);
    }

  private:
    /* Put all member variables and private methods here. These may change arbitrarily. */

    static size_t findClosing(std::string str, char parOpen, char parClose)
    {
      size_t countPar = 1;
      size_t count = 0;
      while (countPar > 0) {
        count++;
        if (str[count] == parOpen) countPar++;
        if (str[count] == parClose) countPar--;
      }
      return count;
    }
  };

} // namespace TempLat

#endif