Skip to content

Commit 93b4f66

Browse files
committed
word streaming
1 parent bf20f89 commit 93b4f66

1 file changed

Lines changed: 26 additions & 11 deletions

File tree

libs/braillify/src/lib.rs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,16 @@ pub struct Encoder {
3232
is_english: bool,
3333
triple_big_english: bool,
3434
english_indicator: bool,
35+
has_processed_word: bool,
3536
}
3637

3738
impl Encoder {
3839
pub fn new(english_indicator: bool) -> Self {
3940
Self {
41+
english_indicator,
4042
is_english: false,
4143
triple_big_english: false,
42-
english_indicator,
44+
has_processed_word: false,
4345
}
4446
}
4547

@@ -49,9 +51,12 @@ impl Encoder {
4951
.filter(|word| !word.is_empty())
5052
.collect::<Vec<&str>>();
5153

52-
let word_count = words.len();
54+
let mut word: &str = "";
55+
let mut remaining_words = &words[..];
56+
while !remaining_words.is_empty() {
57+
let prev_word = word;
58+
(word, remaining_words) = remaining_words.split_first().unwrap();
5359

54-
for (idx, word) in words.iter().enumerate() {
5560
let mut skip_count = 0;
5661

5762
if let Some((_, code, rest)) = word_shortcut::split_word_shortcut(word) {
@@ -77,10 +82,11 @@ impl Encoder {
7782
}
7883

7984
if is_all_uppercase && !self.triple_big_english {
80-
if (idx == 0 || !words[idx - 1].chars().all(|c| c.is_ascii_alphabetic()))
81-
&& word_count - idx > 2
82-
&& words[idx + 1].chars().all(|c| c.is_ascii_alphabetic())
83-
&& words[idx + 2].chars().all(|c| c.is_ascii_alphabetic())
85+
if (!self.has_processed_word
86+
|| !prev_word.chars().all(|c| c.is_ascii_alphabetic()))
87+
&& remaining_words.len() >= 2
88+
&& remaining_words[0].chars().all(|c| c.is_ascii_alphabetic())
89+
&& remaining_words[1].chars().all(|c| c.is_ascii_alphabetic())
8490
{
8591
self.triple_big_english = true;
8692
result.push(32);
@@ -325,8 +331,8 @@ impl Encoder {
325331
}
326332

327333
if self.triple_big_english {
328-
if !(word_count - idx > 1
329-
&& words[idx + 1].chars().all(|c| c.is_ascii_alphabetic()))
334+
if !(remaining_words.len() > 0
335+
&& remaining_words[0].chars().all(|c| c.is_ascii_alphabetic()))
330336
{
331337
// 28항 [붙임] 로마자가 한 글자만 대문자일 때에는 대문자 기호표 ⠠을 그 앞에 적고, 단어 전체가 대문자이거나 두 글자 이상 연속해서 대문자일 때에는 대문자 단어표
332338
// ⠠⠠을 그 앞에 적는다. 세 개 이상의 연속된 단어가 모두 대문자일 때에는 첫 단어
@@ -336,9 +342,13 @@ impl Encoder {
336342
self.triple_big_english = false; // Reset after adding terminator
337343
}
338344
}
339-
if idx != word_count - 1 {
345+
if !remaining_words.is_empty() {
340346
if self.english_indicator
341-
&& !words[idx + 1].chars().next().unwrap().is_ascii_alphabetic()
347+
&& !remaining_words[0]
348+
.chars()
349+
.next()
350+
.unwrap()
351+
.is_ascii_alphabetic()
342352
{
343353
// 제31항 국어 문장 안에 그리스 문자가 나올 때에는 그 앞에 로마자표 ⠴을 적고 그 뒤에 로마자 종료표 ⠲을 적는다
344354
if self.is_english {
@@ -349,6 +359,11 @@ impl Encoder {
349359

350360
result.push(0);
351361
}
362+
363+
// Update state for next iteration
364+
if !self.has_processed_word {
365+
self.has_processed_word = true;
366+
}
352367
}
353368
Ok(())
354369
}

0 commit comments

Comments
 (0)