Repository: tesseract-ocr/tesseract
File: src/ccutil/helpers.h (C/C++)

Code before:
 ** distributed under the License is distributed on an "AS IS" BASIS,
 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 ** See the License for the specific language governing permissions and
 ** limitations under the License.
 *
 *****************************************************************************/

#ifndef TESSERACT_CCUTIL_HELPERS_H_
#define TESSERACT_CCUTIL_HELPERS_H_

#include <cassert>
#include <climits> // for INT_MIN, INT_MAX
#include <cmath> // std::isfinite
#include <cstdio>
#include <cstring>
#include <algorithm>  // for std::find
#include <functional>
#include <random>
#include <string>
#include <vector>

#include "serialis.h"

namespace tesseract {

// Copy a std::string to a newly allocated char *.
inline char *copy_string(const std::string &from) {
  auto length = from.length();
  char *target_string = new char[length + 1];
  from.copy(target_string, length);
  target_string[length] = '\0';
  return target_string;
}

template <class T>
inline bool contains(const std::vector<T> &data, const T &value) {
  return std::find(data.begin(), data.end(), value) != data.end();
}

inline const std::vector<std::string> split(const std::string &s, char c) {
  std::string buff;
  std::vector<std::string> v;
  for (auto n : s) {
    if (n != c) {
      buff += n;
    } else if (n == c && !buff.empty()) {
      v.push_back(buff);
      buff.clear();
    }
  }
  if (!buff.empty()) {

Review comment (refactor):
```suggestion
// Copy a std::string to a newly allocated char *.
// TODO: Remove this function once the related code has been converted to use std::string.
```

Code after:
 ** distributed under the License is distributed on an "AS IS" BASIS,
 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 ** See the License for the specific language governing permissions and
 ** limitations under the License.
 *
 *****************************************************************************/

#ifndef TESSERACT_CCUTIL_HELPERS_H_
#define TESSERACT_CCUTIL_HELPERS_H_

#include <cassert>
#include <climits> // for INT_MIN, INT_MAX
#include <cmath> // std::isfinite
#include <cstdio>
#include <cstring>
#include <algorithm>  // for std::find
#include <functional>
#include <random>
#include <string>
#include <vector>

#include "serialis.h"

namespace tesseract {

// Copy a std::string to a newly allocated char *.
// TODO: Remove this function once the related code has been converted
// to use std::string.
inline char *copy_string(const std::string &from) {
  auto length = from.length();
  char *target_string = new char[length + 1];
  from.copy(target_string, length);
  target_string[length] = '\0';
  return target_string;
}

template <class T>
inline bool contains(const std::vector<T> &data, const T &value) {
  return std::find(data.begin(), data.end(), value) != data.end();
}

inline const std::vector<std::string> split(const std::string &s, char c) {
  std::string buff;
  std::vector<std::string> v;
  for (auto n : s) {
    if (n != c) {
      buff += n;
    } else if (n == c && !buff.empty()) {
      v.push_back(buff);
      buff.clear();
    }