Lumesh String Module

wiki libs

Lumesh String Processing Module Documentation

Module Path: string
Function Description: Provides comprehensive string manipulation capabilities, including character property detection, text splitting, format conversion, and advanced processing. Supports two calling styles:

  • Functional Call: string.func(arg0, arg1...)
  • Imperative Call: string.func arg0 arg1...

Function Categories and Detailed Description

I. Character Property Detection
  1. string.is_empty

    • Functionality: Checks if the string is entirely composed of whitespace characters.
    • Parameters: text: string
    • Return Value: boolean
    • Example:
      string.is_empty ""  # => true
  2. string.is_whitespace

    • Functionality: Checks if the string is entirely composed of whitespace characters.
    • Parameters: text: string
    • Return Value: boolean
    • Example:
      string.is_whitespace " \t\n"  # => true
  3. string.is_alpha

    • Functionality: Checks if the string consists entirely of letters.
    • Parameters: text: string
    • Return Value: boolean
    • Example:
      string.is_alpha "Hello"  # => true
  4. string.is_alphanumeric

    • Functionality: Checks if the string consists entirely of letters or digits.
    • Parameters: text: string
    • Return Value: boolean
    • Example:
      string.is_alphanumeric "R2D2"  # => true
  5. string.is_numeric

    • Functionality: Checks if the string consists entirely of digits.
    • Parameters: text: string
    • Return Value: boolean
    • Example:
      string.is_numeric "³Ⅳ"  # => true (supports Unicode digits)
  6. string.is_lower

    • Functionality: Checks if the string consists entirely of lowercase letters.
    • Parameters: text: string
    • Return Value: boolean
    • Example:
      string.is_lower "hello"  # => true
  7. string.is_upper

    • Functionality: Checks if the string consists entirely of uppercase letters.
    • Parameters: text: string
    • Return Value: boolean
    • Example:
      string.is_upper "HELLO"  # => true
  8. string.is_title

    • Functionality: Checks if the string is in title case (first letter of each word capitalized).
    • Parameters: text: string
    • Return Value: boolean
    • Example:
      string.is_title "Hello World"  # => true

II. Substring Checks
  1. string.starts_with

    • Functionality: Checks if the string starts with the specified substring.
    • Parameters:
      • prefix: string
      • text: string
    • Return Value: boolean
    • Example:
      string.starts_with "He" "Hello"  # => true
  2. string.ends_with

    • Functionality: Checks if the string ends with the specified substring.
    • Parameters:
      • suffix: string
      • text: string
    • Return Value: boolean
    • Example:
      string.ends_with "lo" "Hello"  # => true
  3. string.contains

    • Functionality: Checks if the string contains the specified substring.
    • Parameters:
      • substring: string
      • text: string
    • Return Value: boolean
    • Example:
      string.contains "ell" "Hello"  # => true

III. Text Splitting
  1. string.split

    • Functionality: Splits the string by a delimiter.
    • Parameters:
      • delimiter: string
      • text: string
    • Return Value: [part1, part2, ...]
    • Example:
      string.split ";" "a;b;c"  # => ["a", "b", "c"]
  2. string.split_at

    • Functionality: Splits the string at the specified index.
    • Parameters:
      • index: integer
      • text: string
    • Return Value: [left_part, right_part]
    • Example:
      string.split_at 2 "Hello"  # => ["He", "llo"]
  3. string.chars

    • Functionality: Splits the string into a list of characters.
    • Parameters: text: string
    • Return Value: [char1, char2, ...]
    • Example:
      string.chars "ABC"  # => ["A", "B", "C"]
  4. string.words

    • Functionality: Splits the string into words by whitespace.
    • Parameters: text: string
    • Return Value: [word1, word2, ...]
    • Example:
      string.words "hello world"  # => ["hello", "world"]
  5. string.lines

    • Functionality: Splits the text into lines by newline characters.
    • Parameters: text: string
    • Return Value: [line1, line2, ...]
    • Example:
      string.lines "a\nb"  # => ["a", "b"]
  6. string.paragraphs

    • Functionality: Splits the text into paragraphs by empty lines.
    • Parameters: text: string
    • Return Value: [para1, para2, ...]
    • Example:
      string.paragraphs "a\n\nb"  # => ["a", "b"]

IV. Text Modification
  1. string.repeat

    • Functionality: Repeats the string a specified number of times.
    • Parameters:
      • count: integer
      • text: string
    • Return Value: string
    • Example:
      string.repeat 3 "a"  # => "aaa"
  2. string.replace

    • Functionality: Replaces all matching substrings.
    • Parameters:
      • from: string
      • to: string
      • text: string
    • Return Value: string
    • Example:
      string.replace "l" "x" "hello"  # => "hexxo"
  3. string.substring

    • Functionality: Extracts a substring.
    • Parameters:
      • start: integer (supports negative indexing)
      • end: integer (optional, supports negative indexing)
      • text: string
    • Return Value: string
    • Example:
      string.substring 1 3 "Hello"  # => "el"
      string.substring -3 -1 "Hello" # => "ll"
  4. string.remove_prefix

    • Functionality: Removes the prefix if it exists.
    • Parameters:
      • prefix: string
      • text: string
    • Return Value: string
    • Example:
      string.remove_prefix "He" "Hello"  # => "llo"
  5. string.remove_suffix

    • Functionality: Removes the suffix if it exists.
    • Parameters:
      • suffix: string
      • text: string
    • Return Value: string
    • Example:
      string.remove_suffix "lo" "Hello"  # => "Hel"
  6. string.trim

    • Functionality: Removes whitespace from both ends.
    • Parameters: text: string
    • Return Value: string
    • Example:
      string.trim " a "  # => "a"
  7. string.trim_start

    • Functionality: Removes whitespace from the start.
    • Parameters: text: string
    • Return Value: string
    • Example:
      string.trim_start " a"  # => "a"
  8. string.trim_end

    • Functionality: Removes whitespace from the end.
    • Parameters: text: string
    • Return Value: string
    • Example:
      string.trim_end "a "  # => "a"
  9. string.to_lower

    • Functionality: Converts to lowercase.
    • Parameters: text: string
    • Return Value: string
    • Example:
      string.to_lower "HeLLo"  # => "hello"
  10. string.to_upper
    - Functionality: Converts to uppercase.
    - Parameters: text: string
    - Return Value: string
    - Example:

    string.to_upper "hello"  # => "HELLO"
  11. string.to_title
    - Functionality: Converts to title case.
    - Parameters: text: string
    - Return Value: string
    - Example:

    string.to_title "hello world"  # => "Hello World"

V. Advanced Features
  1. string.caesar

    • Functionality: Encrypts using Caesar cipher.
    • Parameters:
      • shift: integer (optional, default is 13)
      • text: string
    • Return Value: string
    • Example:
      string.caesar "ABC"  # => "NOP" (default shift 13)
      string.caesar 13 "ABC" # => "DEF"
  2. string.get_width

    • Functionality: Gets the maximum line width of the text.
    • Parameters: text: string
    • Return Value: integer
    • Example:
      string.get_width "a\nbb\nccc"  # => 3

General Rules

  1. Index Handling:

    • Positive indices start at 0.
    • Throws an exception for out-of-bounds indices.
  2. Whitespace Definition:

    • Includes spaces, tabs, newline characters, and other Unicode whitespace characters.
  3. Error Handling:

    • Throws an exception for parameter type errors.