Rune Caster 1.0.0
Modern C++ Text Processing Framework
Loading...
Searching...
No Matches
whitespace_normalizer.cpp
Go to the documentation of this file.
3#include <string>
4#include <iterator>
5
6namespace rune_caster {
7namespace spell {
8namespace core {
9
10WhitespaceNormalizer::WhitespaceNormalizer(bool collapse_multiple, bool trim_edges)
11 : collapse_multiple_(collapse_multiple), trim_edges_(trim_edges) {
12}
13
15 if (input.empty()) {
16 return RuneSequence();
17 }
18
19 RuneSequence temp_result;
20 temp_result.reserve(input.size());
21
22 // 1단계: 모든 공백을 표준 공백으로 정규화하고 collapse_multiple_ 적용
23 bool prev_was_whitespace = false;
24
25 for (const auto& rune : input) {
26 if (rune.is_whitespace()) {
27 // 모든 공백 문자(탭, 개행 등)를 표준 공백으로 정규화
28 if (collapse_multiple_) {
29 // 연속된 공백을 하나로 축소
30 if (!prev_was_whitespace) {
31 temp_result.emplace_back(U' ', rune.language());
32 }
33 } else {
34 // 공백 축소 없이 정규화만
35 temp_result.emplace_back(U' ', rune.language());
36 }
37 prev_was_whitespace = true;
38 } else {
39 // 일반 문자는 그대로 추가
40 temp_result.push_back(rune);
41 prev_was_whitespace = false;
42 }
43 }
44
45 // 2단계: trim_edges_ 적용
46 if (!trim_edges_) {
47 return temp_result;
48 }
49
50 // 앞쪽 공백 제거
51 auto start_it = temp_result.begin();
52 while (start_it != temp_result.end() && start_it->is_whitespace()) {
53 ++start_it;
54 }
55
56 // 뒤쪽 공백 제거
57 auto end_it = temp_result.end();
58 while (end_it != start_it && (end_it - 1)->is_whitespace()) {
59 --end_it;
60 }
61
62 // 결과 생성
63 RuneSequence result;
64 if (start_it < end_it) {
65 // assign 메서드 대신 반복자를 사용해서 복사
66 result.reserve(std::distance(start_it, end_it));
67 for (auto it = start_it; it != end_it; ++it) {
68 result.push_back(*it);
69 }
70 }
71
72 return result;
73}
74
76 std::string desc = "Whitespace normalizer (";
77 if (collapse_multiple_) {
78 desc += "collapse multiple";
79 if (trim_edges_) desc += ", ";
80 }
81 if (trim_edges_) {
82 desc += "trim edges";
83 }
84 desc += ")";
85 return desc;
86}
87
88} // namespace core
89} // namespace spell
90} // namespace rune_caster
reference emplace_back(Args &&... args)
bool empty() const noexcept
void reserve(size_type new_cap)
void push_back(const Rune &rune)
size_type size() const noexcept
iterator end() noexcept
iterator begin() noexcept
WhitespaceNormalizer(bool collapse_multiple=true, bool trim_edges=true)
Construct a WhitespaceNormalizer.
std::string description() const override
Get the spell's description.
RuneSequence operator()(const RuneSequence &input) const override
Apply the spell transformation.
RuneString RuneSequence
Backward compatibility alias for RuneString.
Simple and unified spell system for text transformation.