Home / File/ utf16.rs — tailwindcss Source File

utf16.rs — tailwindcss Source File

Architecture documentation for utf16.rs, a rust file in the tailwindcss codebase. 2 imports, 0 dependents.

File rust NodeBridge NapiBinding 2 imports 3 functions

Entity Profile

Dependency Diagram

graph LR
  7a49803e_c498_1a8f_e430_3827f8691042["utf16.rs"]
  3e6361b5_cf94_5595_e435_45345366da41["super::*"]
  7a49803e_c498_1a8f_e430_3827f8691042 --> 3e6361b5_cf94_5595_e435_45345366da41
  de6574c4_9ce4_b384_b497_36f00fafcc61["std::collections::HashMap"]
  7a49803e_c498_1a8f_e430_3827f8691042 --> de6574c4_9ce4_b384_b497_36f00fafcc61
  style 7a49803e_c498_1a8f_e430_3827f8691042 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/// The `IndexConverter` is used to convert UTF-8 *BYTE* indexes to UTF-16
/// *character* indexes
#[derive(Clone)]
pub struct IndexConverter<'a> {
  input: &'a str,
  curr_utf8: usize,
  curr_utf16: usize,
}

impl<'a> IndexConverter<'a> {
  pub fn new(input: &'a str) -> Self {
    Self {
      input,
      curr_utf8: 0,
      curr_utf16: 0,
    }
  }

  pub fn get(&mut self, pos: usize) -> i64 {
    #[cfg(debug_assertions)]
    if self.curr_utf8 > self.input.len() {
      panic!("curr_utf8 points past the end of the input string");
    }

    if pos < self.curr_utf8 {
      self.curr_utf8 = 0;
      self.curr_utf16 = 0;
    }

    // SAFETY: No matter what `pos` is passed into this function `curr_utf8`
    // will only ever be incremented up to the length of the input string.
    //
    // This eliminates a "potential" panic that cannot actually happen
    let slice = unsafe { self.input.get_unchecked(self.curr_utf8..) };

    for c in slice.chars() {
      if self.curr_utf8 >= pos {
        break;
      }

      self.curr_utf8 += c.len_utf8();
      self.curr_utf16 += c.len_utf16();
    }

    self.curr_utf16 as i64
  }
}

#[cfg(test)]
mod test {
  use super::*;
  use std::collections::HashMap;

  #[test]
  fn test_index_converter() {
    let mut converter = IndexConverter::new("Hello 🔥🥳 world!");

    let map = HashMap::from([
      // hello<space>
      (0, 0),
      (1, 1),
      (2, 2),
      (3, 3),
      (4, 4),
      (5, 5),
      (6, 6),
      // inside the 🔥
      (7, 8),
      (8, 8),
      (9, 8),
      (10, 8),
      // inside the 🥳
      (11, 10),
      (12, 10),
      (13, 10),
      (14, 10),
      // <space>world!
      (15, 11),
      (16, 12),
      (17, 13),
      (18, 14),
      (19, 15),
      (20, 16),
      (21, 17),
      // Past the end should return the last utf-16 character index
      (22, 17),
      (100, 17),
    ]);

    for (idx_utf8, idx_utf16) in map {
      assert_eq!(converter.get(idx_utf8), idx_utf16);
    }
  }
}

Domain

Subdomains

Dependencies

  • std::collections::HashMap
  • super::*

Frequently Asked Questions

What does utf16.rs do?
utf16.rs is a source file in the tailwindcss codebase, written in rust. It belongs to the NodeBridge domain, NapiBinding subdomain.
What functions are defined in utf16.rs?
utf16.rs defines 3 function(s): get, new, test_index_converter.
What does utf16.rs depend on?
utf16.rs imports 2 module(s): std::collections::HashMap, super::*.
Where is utf16.rs in the architecture?
utf16.rs is located at crates/node/src/utf16.rs (domain: NodeBridge, subdomain: NapiBinding, directory: crates/node/src).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free