splice

Simple text splicing module for MoonBit

text
splice
template
string
moon add Yoorkin/splice@0.1.2
Download zip
Author
Version
0.1.2
License
Apache-2.0
Last updated
10 months ago
Downloads
1K
README

#Yoorkin/splice

A MoonBit module for splicing substrings in a template string at specified ranges with given replacements.

#
PosUnit

pub(all) enum PosUnit {
Unicode
}

impl Compare for PosUnit
impl Eq for PosUnit

#
Replacement

type Replacement

impl Eq for Replacement

#
char_at

fn char_at(line : Int, column : Int, replacement : String, unit? : PosUnit) -> Replacement

Creates a Replacement for a char in the text.

Parameters

  • line: The line number (0-based, in Unicode characters) where the replacement occurs.
  • column: The column number (0-based, in Unicode characters) where the replacement occurs.
  • replacement: The string to insert at the specified point.

#
multiline

fn multiline(start : (Int, Int), end : (Int, Int), replacement : String, unit? : PosUnit) -> Replacement

Creates a Replacement for a range that spans multiple lines.

Parameters

  • start: A tuple (line, column) representing the start of the range (0-based, in Unicode characters).
  • end: A tuple (line, column) representing the end of the range (0-based, in Unicode characters, inclusive).
  • replacement: The string to replace the specified range.

#
point

fn point(line : Int, column : Int, replacement : String, unit? : PosUnit) -> Replacement

Creates a Replacement for a single point in the text, referring to the left side of the character.

Parameters

  • line: The line number (0-based, in Unicode characters) where the replacement occurs.
  • column: The column number (0-based, in Unicode characters) where the replacement occurs.
  • replacement: The string to insert at the specified position.

#
range

fn range(line : Int, start_column : Int, end_column : Int, replacement : String, unit? : PosUnit) -> Replacement

Creates a Replacement for a range in a single line.

Parameters

  • line: The line number (0-based, in Unicode characters) where the replacement occurs.
  • start_column: The starting column number (0-based, in Unicode characters) of the range.
  • end_column: The ending column number (0-based, in Unicode characters, inclusive) of the range.
  • replacement: The string to replace the specified range.

#
splice

fn splice(template : StringView, replacements : Array[Replacement]) -> String

Splices substrings in template at the specified ranges with the given replacements.

The replacement ranges must not overlap.

Parameters

  • template: The original string to perform splicing on.
  • replacements: An array of Replacement structs specifying the ranges and their replacements.

Returns

  • The modified string after applying all replacements.

Exceptions

  • Raises an Error if any of the specified ranges are invalid or out of bounds.

Examples

let template = #|fn get(🐰){ #| ... #| ... #| h($) #|} inspect( splice(template, [ multiline((1, 1), (2, 3), "f(x)\n g()"), range(0, 7, 7, "x : Int"), point(0, 9, " -> Int "), char_at(3, 3, "1001"), ]), content=( #|fn get(x : Int) -> Int { #| f(x) #| g() #| h(1001) #|} ), )

Source Files