/src/serenity/Userland/Libraries/LibUnicode/Segmentation.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2022, Idan Horowitz <idan.horowitz@serenityos.org> |
3 | | * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org> |
4 | | * |
5 | | * SPDX-License-Identifier: BSD-2-Clause |
6 | | */ |
7 | | |
8 | | #pragma once |
9 | | |
10 | | #include <AK/Forward.h> |
11 | | #include <AK/Function.h> |
12 | | #include <AK/IterationDecision.h> |
13 | | #include <AK/Optional.h> |
14 | | #include <AK/Types.h> |
15 | | |
16 | | namespace Unicode { |
17 | | |
18 | | using SegmentationCallback = Function<IterationDecision(size_t)>; |
19 | | |
20 | | void for_each_grapheme_segmentation_boundary(Utf8View const&, SegmentationCallback); |
21 | | void for_each_grapheme_segmentation_boundary(Utf16View const&, SegmentationCallback); |
22 | | void for_each_grapheme_segmentation_boundary(Utf32View const&, SegmentationCallback); |
23 | | |
24 | | template<typename ViewType> |
25 | | Optional<size_t> next_grapheme_segmentation_boundary(ViewType const& view, size_t index) |
26 | | { |
27 | | Optional<size_t> result; |
28 | | |
29 | | for_each_grapheme_segmentation_boundary(view, [&](auto boundary) { |
30 | | if (boundary > index) { |
31 | | result = boundary; |
32 | | return IterationDecision::Break; |
33 | | } |
34 | | |
35 | | return IterationDecision::Continue; |
36 | | }); |
37 | | |
38 | | return result; |
39 | | } |
40 | | |
41 | | template<typename ViewType> |
42 | | Optional<size_t> previous_grapheme_segmentation_boundary(ViewType const& view, size_t index) |
43 | | { |
44 | | Optional<size_t> result; |
45 | | |
46 | | for_each_grapheme_segmentation_boundary(view, [&](auto boundary) { |
47 | | if (boundary < index) { |
48 | | result = boundary; |
49 | | return IterationDecision::Continue; |
50 | | } |
51 | | |
52 | | return IterationDecision::Break; |
53 | | }); |
54 | | |
55 | | return result; |
56 | | } |
57 | | |
58 | | void for_each_word_segmentation_boundary(Utf8View const&, SegmentationCallback); |
59 | | void for_each_word_segmentation_boundary(Utf16View const&, SegmentationCallback); |
60 | | void for_each_word_segmentation_boundary(Utf32View const&, SegmentationCallback); |
61 | | |
62 | | template<typename ViewType> |
63 | | Optional<size_t> next_word_segmentation_boundary(ViewType const& view, size_t index) |
64 | 0 | { |
65 | 0 | Optional<size_t> result; |
66 | |
|
67 | 0 | for_each_word_segmentation_boundary(view, [&](auto boundary) { |
68 | 0 | if (boundary > index) { |
69 | 0 | result = boundary; |
70 | 0 | return IterationDecision::Break; |
71 | 0 | } |
72 | | |
73 | 0 | return IterationDecision::Continue; |
74 | 0 | }); |
75 | |
|
76 | 0 | return result; |
77 | 0 | } |
78 | | |
79 | | template<typename ViewType> |
80 | | Optional<size_t> previous_word_segmentation_boundary(ViewType const& view, size_t index) |
81 | | { |
82 | | Optional<size_t> result; |
83 | | |
84 | | for_each_word_segmentation_boundary(view, [&](auto boundary) { |
85 | | if (boundary < index) { |
86 | | result = boundary; |
87 | | return IterationDecision::Continue; |
88 | | } |
89 | | |
90 | | return IterationDecision::Break; |
91 | | }); |
92 | | |
93 | | return result; |
94 | | } |
95 | | |
96 | | void for_each_sentence_segmentation_boundary(Utf8View const&, SegmentationCallback); |
97 | | void for_each_sentence_segmentation_boundary(Utf16View const&, SegmentationCallback); |
98 | | void for_each_sentence_segmentation_boundary(Utf32View const&, SegmentationCallback); |
99 | | |
100 | | template<typename ViewType> |
101 | | Optional<size_t> next_sentence_segmentation_boundary(ViewType const& view, size_t index) |
102 | | { |
103 | | Optional<size_t> result; |
104 | | |
105 | | for_each_sentence_segmentation_boundary(view, [&](auto boundary) { |
106 | | if (boundary > index) { |
107 | | result = boundary; |
108 | | return IterationDecision::Break; |
109 | | } |
110 | | |
111 | | return IterationDecision::Continue; |
112 | | }); |
113 | | |
114 | | return result; |
115 | | } |
116 | | |
117 | | template<typename ViewType> |
118 | | Optional<size_t> previous_sentence_segmentation_boundary(ViewType const& view, size_t index) |
119 | | { |
120 | | Optional<size_t> result; |
121 | | |
122 | | for_each_sentence_segmentation_boundary(view, [&](auto boundary) { |
123 | | if (boundary < index) { |
124 | | result = boundary; |
125 | | return IterationDecision::Continue; |
126 | | } |
127 | | |
128 | | return IterationDecision::Break; |
129 | | }); |
130 | | |
131 | | return result; |
132 | | } |
133 | | |
134 | | } |