Welcome to mirror list, hosted at ThFree Co, Russian Federation.

replace_digits.hpp « src - github.com/moses-smt/nplm.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e8ac957162419550f0928e6d05e8124464dd4b1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/** \file \author Jonathan Graehl <graehl@gmail.com>

    replace 0-9 ascii chars with another ascii replacement

    To the extent possible under law, the author(s) have dedicated all copyright
    and related and neighboring rights to this software to the public domain
    worldwide. This software is distributed without any warranty.
*/

#ifndef REPLACEDIGITS_GRAEHL_2015_06_25_H
#define REPLACEDIGITS_GRAEHL_2015_06_25_H
#pragma once

#include <string>
#include <utility>

namespace graehl {

inline bool ascii_digit(char c) {
  return c >= '0' && c <= '9';
}

struct replace_digits {
  char map_digits;
  replace_digits(char map_digits = '@') : map_digits(map_digits) {}

  /// \return whether anything was replaced
  bool replaced(char* i, char* end) const {
    for (; i != end; ++i)
      if (ascii_digit(*i)) {
        *i = map_digits;
        while (++i != end)
          if (ascii_digit(*i)) *i = map_digits;
        return true;
      }
    return false;
  }
  /// maybe: only if non-0 map_digits, do the thing
  bool maybe_replaced(char* i, char* end) const { return map_digits && replaced(i, end); }

  void replace(char* i, char* end) const {
    for (; i != end; ++i)
      if (ascii_digit(*i)) *i = map_digits;
  }
  void maybe_replace(char* i, char* end) const {
    if (map_digits) replace(i, end);
  }

  void replace(std::string& str, std::string::size_type i = 0) const {
    std::string::size_type n = str.size();
    char* d = (char *)str.data(); // although only C++11 officially allows this, in reality everyone does
    replace(d + i, d + n);
  }
  void maybe_replace(std::string& str, std::string::size_type i = 0) const {
    if (map_digits) replace(str, i);
  }
};


}

#endif