txt_chunk

Crates.iotxt_chunk
lib.rstxt_chunk
version0.1.2
created_at2025-07-16 08:53:08.342856+00
updated_at2025-08-05 08:17:34.129288+00
descriptiontxt_chunk
homepagehttps://github.com/i18n-site/rust/tree/dev/txt_chunk
repositoryhttps://github.com/i18n-site/rust.git
max_upload_size
id1755199
size22,337
i18n.site (i18nsite)

documentation

README

txt_chunk

pub type ChunkLi = Vec<Vec<String>>;

pub fn txt_chunk<S: Into<String>>(li: impl IntoIterator<Item = S>, limit: usize) -> ChunkLi {
  let mut r = vec![];
  let mut t = vec![];
  let mut len = 0;
  for i in li {
    let i = i.into();
    let i_len = 1 + i.len();
    let next_len = len + i_len;
    if next_len > limit {
      r.push(t);
      if i_len < limit {
        t = vec![i];
        len = i_len;
      } else {
        let mut end = limit;
        while !i.is_char_boundary(end) && end > 0 {
          end -= 1;
        }
        if end > 0 {
          r.push(vec![i[..end].into()]);
        }
        t = vec![];
        len = 0;
      }
    } else {
      len = next_len;
      t.push(i);
    }
  }

  if !t.is_empty() {
    r.push(t);
  }

  r
}

About

This project is an open-source component of i18n.site ⋅ Internationalization Solution.

关于

本项目为 i18n.site ⋅ 国际化解决方案 的开源组件。

Commit count: 68

cargo fmt