Home / File/ segment.ts — tailwindcss Source File

segment.ts — tailwindcss Source File

Architecture documentation for segment.ts, a typescript file in the tailwindcss codebase. 0 imports, 27 dependents.

File typescript Oxide Scanner 27 dependents 1 functions

Entity Profile

Dependency Diagram

graph LR
  bb9924cc_8308_a1f9_0e30_76de45a64970["segment.ts"]
  c30c28f2_b6df_e90d_67d1_db074bef35a5["analyze.ts"]
  c30c28f2_b6df_e90d_67d1_db074bef35a5 --> bb9924cc_8308_a1f9_0e30_76de45a64970
  587045dd_6997_cdb6_9548_cd81dc79a115["migrate-at-apply.ts"]
  587045dd_6997_cdb6_9548_cd81dc79a115 --> bb9924cc_8308_a1f9_0e30_76de45a64970
  7de2c7a6_0dcf_0fb9_b7dd_c51f75983852["migrate-at-layer-utilities.ts"]
  7de2c7a6_0dcf_0fb9_b7dd_c51f75983852 --> bb9924cc_8308_a1f9_0e30_76de45a64970
  4110edba_26d6_f493_077a_dd261e1ea456["migrate-import.ts"]
  4110edba_26d6_f493_077a_dd261e1ea456 --> bb9924cc_8308_a1f9_0e30_76de45a64970
  c8e9db66_5242_9932_2886_2c9db99f6ac2["migrate-missing-layers.ts"]
  c8e9db66_5242_9932_2886_2c9db99f6ac2 --> bb9924cc_8308_a1f9_0e30_76de45a64970
  a3df83e7_b177_b92b_d91f_ff6ea6bba20c["migrate-legacy-arbitrary-values.ts"]
  a3df83e7_b177_b92b_d91f_ff6ea6bba20c --> bb9924cc_8308_a1f9_0e30_76de45a64970
  e4e5ac2c_0d58_689d_0ad6_7086823787f1["migrate-prefix.ts"]
  e4e5ac2c_0d58_689d_0ad6_7086823787f1 --> bb9924cc_8308_a1f9_0e30_76de45a64970
  c36efdeb_7fd2_0935_2c28_bf15095a9dd9["migrate-theme-to-var.ts"]
  c36efdeb_7fd2_0935_2c28_bf15095a9dd9 --> bb9924cc_8308_a1f9_0e30_76de45a64970
  1721da0c_9e1d_5bee_ab0a_a192cfa6640d["apply.ts"]
  1721da0c_9e1d_5bee_ab0a_a192cfa6640d --> bb9924cc_8308_a1f9_0e30_76de45a64970
  ba6fca27_7720_5839_0f92_bc2abb8db636["candidate.ts"]
  ba6fca27_7720_5839_0f92_bc2abb8db636 --> bb9924cc_8308_a1f9_0e30_76de45a64970
  f6c14bbb_2e42_58cc_18f1_c89a243da9c0["canonicalize-candidates.ts"]
  f6c14bbb_2e42_58cc_18f1_c89a243da9c0 --> bb9924cc_8308_a1f9_0e30_76de45a64970
  2745c5e0_8ea4_a1c7_4f84_369746e3eb63["apply-compat-hooks.ts"]
  2745c5e0_8ea4_a1c7_4f84_369746e3eb63 --> bb9924cc_8308_a1f9_0e30_76de45a64970
  4b9fb355_7055_4825_6db8_a5dd6f45c06e["default-theme.ts"]
  4b9fb355_7055_4825_6db8_a5dd6f45c06e --> bb9924cc_8308_a1f9_0e30_76de45a64970
  964be968_c10d_edf8_0061_9ac8db9f9935["legacy-utilities.ts"]
  964be968_c10d_edf8_0061_9ac8db9f9935 --> bb9924cc_8308_a1f9_0e30_76de45a64970
  style bb9924cc_8308_a1f9_0e30_76de45a64970 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

const BACKSLASH = 0x5c
const OPEN_CURLY = 0x7b
const CLOSE_CURLY = 0x7d
const OPEN_PAREN = 0x28
const CLOSE_PAREN = 0x29
const OPEN_BRACKET = 0x5b
const CLOSE_BRACKET = 0x5d
const DOUBLE_QUOTE = 0x22
const SINGLE_QUOTE = 0x27

// This is a shared buffer that is used to keep track of the current nesting level
// of parens, brackets, and braces. It is used to determine if a character is at
// the top-level of a string. This is a performance optimization to avoid memory
// allocations on every call to `segment`.
const closingBracketStack = new Uint8Array(256)

/**
 * This splits a string on a top-level character.
 *
 * Regex doesn't support recursion (at least not the JS-flavored version),
 * so we have to use a tiny state machine to keep track of paren placement.
 *
 * Expected behavior using commas:
 * var(--a, 0 0 1px rgb(0, 0, 0)), 0 0 1px rgb(0, 0, 0)
 *        ┬              ┬  ┬    ┬
 *        x              x  x    ╰──────── Split because top-level
 *        ╰──────────────┴──┴───────────── Ignored b/c inside >= 1 levels of parens
 */
export function segment(input: string, separator: string) {
  // SAFETY: We can use an index into a shared buffer because this function is
  // synchronous, non-recursive, and runs in a single-threaded environment.
  let stackPos = 0
  let parts: string[] = []
  let lastPos = 0
  let len = input.length

  let separatorCode = separator.charCodeAt(0)

  for (let idx = 0; idx < len; idx++) {
    let char = input.charCodeAt(idx)

    if (stackPos === 0 && char === separatorCode) {
      parts.push(input.slice(lastPos, idx))
      lastPos = idx + 1
      continue
    }

    switch (char) {
      case BACKSLASH:
        // The next character is escaped, so we skip it.
        idx += 1
        break
      // Strings should be handled as-is until the end of the string. No need to
      // worry about balancing parens, brackets, or curlies inside a string.
      case SINGLE_QUOTE:
      case DOUBLE_QUOTE:
        // Ensure we don't go out of bounds.
        while (++idx < len) {
          let nextChar = input.charCodeAt(idx)

          // The next character is escaped, so we skip it.
          if (nextChar === BACKSLASH) {
            idx += 1
            continue
          }

          if (nextChar === char) {
            break
          }
        }
        break
      case OPEN_PAREN:
        closingBracketStack[stackPos] = CLOSE_PAREN
        stackPos++
        break
      case OPEN_BRACKET:
        closingBracketStack[stackPos] = CLOSE_BRACKET
        stackPos++
        break
      case OPEN_CURLY:
        closingBracketStack[stackPos] = CLOSE_CURLY
        stackPos++
        break
      case CLOSE_BRACKET:
      case CLOSE_CURLY:
      case CLOSE_PAREN:
        if (stackPos > 0 && char === closingBracketStack[stackPos - 1]) {
          // SAFETY: The buffer does not need to be mutated because the stack is
          // only ever read from or written to its current position. Its current
          // position is only ever incremented after writing to it. Meaning that
          // the buffer can be dirty for the next use and still be correct since
          // reading/writing always starts at position `0`.
          stackPos--
        }
        break
    }
  }

  parts.push(input.slice(lastPos))

  return parts
}

Domain

Subdomains

Functions

Frequently Asked Questions

What does segment.ts do?
segment.ts is a source file in the tailwindcss codebase, written in typescript. It belongs to the Oxide domain, Scanner subdomain.
What functions are defined in segment.ts?
segment.ts defines 1 function(s): segment.
What files import segment.ts?
segment.ts is imported by 27 file(s): analyze.ts, apply-compat-hooks.ts, apply.ts, brace-expansion.ts, candidate.ts, canonicalize-candidates.ts, css-functions.ts, default-theme.ts, and 19 more.
Where is segment.ts in the architecture?
segment.ts is located at packages/tailwindcss/src/utils/segment.ts (domain: Oxide, subdomain: Scanner, directory: packages/tailwindcss/src/utils).

Analyze Your Own Codebase

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

Try Supermodel Free