Regexp

Go like Regular expressions

regexp

Creates a compiled regular expression object from a pattern string.

Args: 1

pattern String containing a regular expression pattern

returns native regexp object or error if pattern is invalid

regexp "[0-9]" |type?
; returns native
regexp "[0-9]" |kind?
; returns regexp

re

Creates a compiled regular expression object from a pattern string.

Args: 1

pattern String containing a regular expression pattern

returns native regexp object or error if pattern is invalid

re "[0-9]" |type?
; returns native
re "[0-9]" |kind?
; returns regexp

regexp//Is-match

Tests if a string matches the regular expression pattern.

Args: 2

regexp Native regexp object

input String to test against the pattern

returns boolean true if the string matches the pattern, false otherwise

regexp "[0-9]" |Is-match "5"
; returns true
regexp "[0-9]" |Is-match "a"
; returns false

regexp//Submatch?

Extracts the first captured group from a string using the regular expression.

Args: 2

regexp Regular expression with capturing groups

input String to search in

returns string containing the first captured group or error if no submatch found

regexp "x([0-9]+)y" |Submatch? "x123y"
; returns "123"

regexp//Submatches?

Extracts all captured groups from the first match as a block of strings.

Args: 2

regexp Regular expression with capturing groups

input String to search in

returns block containing all captured groups from the first match or error if no match found

regexp "x([0-9]+)y" |Submatches? "x123y x234y"
; returns { "123" }

regexp//Submatches\all?

Extracts all captured groups from all matches as a nested block structure.

Args: 2

regexp Regular expression with capturing groups

input String to search in

returns block of blocks, each inner block containing the captured groups from one match

regexp "x([0-9]+)(y+)?" |Submatches\all? "x11yy x22"
; returns { { "11" "yy" } { "22" "" } }

regexp//Find-all

Finds all substrings matching the regular expression and returns them as a block.

Args: 2

regexp Regular expression pattern

input String to search in

returns block containing all matching substrings or error if no matches found

regexp "[0-9]+" |Find-all "x123y x234y"
; returns { "123" "234" }

regexp//Match?

Finds the first substring matching the regular expression.

Args: 2

regexp Regular expression pattern

input String to search in

returns string containing the first match or empty string if no match found

regexp "[0-9]+c+" |Match? "aa33bb55cc"
; returns "55cc"

regexp//Replace-all

Replaces all occurrences of the regular expression pattern with the specified replacement string.

Args: 3

regexp Regular expression pattern

input String to modify

replacement String to replace matches with

returns string with all matches replaced by the replacement string

regexp "[0-9]+" |Replace-all "x123y x234y" "XXX"
; returns "xXXXy xXXXy"

JSON

Parsing and generating JSON

parse-json

Parses JSON string into Rye values.

Args: 1

json string containing JSON data

returns parsed Rye value (list, dict, string, integer, etc.)

"[ 1, 2, 3 ]" |parse-json |length?
; returns 3
"[ 1, 2, 3 ]" |parse-json |type?
; returns list
"{"name": "John", "age": 30}" |parse-json -> "name"
; returns "John"

parse-json\lines

Parses JSON string into Rye values.

Args: 1

json string containing consecutive JSON values

returns list of parsed Rye values

"{"a": 2, "b": "x"} 
{"a": 3, "b": "y"} 
" |parse-json\lines |to-table
; returns table { "a" "b" } [ 2 "x" 3 "y" ]

to-json

Converts a Rye value to a JSON string. Supports block (like list) and context (like dict).

Args: 1

value any Rye value to encode (block, list, dict, context, string, integer, etc.)

returns string containing the JSON representation

dict { a: 1 b: 2 c: 3 } |to-json |parse-json |-> "b"
; returns 2
context { a: 1 b: 2 } |to-json |parse-json |-> "a"
; returns 1
true |to-json
; returns "true"
false |to-json
; returns "false"

to-json\lines

Converts a table to JSON with each row on a separate line.

Args: 1

table table value to encode

returns string containing the JSON representation with each row on a new line

BSON

BSON encoding and decoding

from-bson

Decodes BSON data into Rye values.

Args: 1

bytes native bytes object containing BSON data

returns decoded Rye value (string, integer, decimal, block, etc.)

"abc" |to-bson |from-bson
; returns "abc"
123 |to-bson |from-bson
; returns 123
{ 123 "asd" } |to-bson |from-bson
; returns { 123 "asd" }

to-bson

Encodes a Rye value into BSON format.

Args: 1

value any Rye value to encode (string, integer, decimal, block, etc.)

returns native bytes object containing the BSON-encoded data

SXML

Streaming, SAX-like XML processing

reader//Do-sxml

Processes XML using a streaming SAX-like approach with tag handlers.

Args: 2

reader XML reader object

block SXML processing block with tag handlers

returns result of processing the XML

"<scene><bot>C3PO</bot><bot>R2D2</bot><jedi>Luke</jedi></scene>" |reader |Do-sxml { bot { _ [ .prns ] } }
; prints "C3PO R2D2 "
"<scene><ship>XWing</ship><bot>R2D2</bot><jedi>Luke</jedi></scene>" |reader |Do-sxml { bot jedi { _ [ .prns ] } }
; prints "R2D2 Luke "
"<scene><xwing><bot>R2D2</bot><person>Luke</person></xwing><destroyer><person>Vader</person></destroyer></scene>" |reader |Do-sxml { xwing { person { _ [ .prns ] } } }
; prints "Luke "

rye-sxml-start//Attr?

Retrieves an attribute by index or name from an XML start element.

Args: 2

element XML start element

index Integer index of the Attribute to retrieve

returns list [ namespace tag value ] of the attribute or void if not found

rye-sxml-start//Attrs?

Returns a dict of all attributes from an XML start element.

Args: 1

element XML start element

returns dict with all attributes where keys are "attrname" or "namespace:attrname"

rye-sxml-start//Name?

Returns the name of an XML start element.

Args: 1

element XML start element

returns string name of the XML element

HTML

HTML processing functions

unescape\html

Converts HTML entities to their character equivalents.

Args: 1

text HTML-escaped string

returns string with HTML entities converted to their character equivalents

unescape\html "&gt;hello&lt;"
; returns ">hello<"

escape\html

Converts special characters to HTML entities.

Args: 1

text String containing HTML special characters

returns string with special characters converted to HTML entities

escape\html "<hello>"
; returns "&lt;hello&gt;"

html->markdown

Converts HTML text to markdown format.

Args: 1

html HTML string to convert

returns string containing markdown equivalent of the HTML

html->markdown "<h1>title</h1><p>para</p>"
; returns "# title
; 
; para"

reader//Parse-html

Parses HTML using a streaming approach with tag handlers.

Args: 2

reader HTML reader object

block HTML processing block with tag handlers

returns result of processing the HTML

rye-html-start//Attr?

Retrieves an attribute value by name or index from an HTML element.

Args: 2

element HTML token element

name-or-index Attribute name (as word or string) or index (as integer)

returns string value of the attribute or void if not found

rye-html-start//Attrs?

Returns a dict of all attributes from an HTML element.

Args: 1

element HTML token element

returns dict with all attributes where keys are attribute names

rye-html-start//Name?

Returns the name of an HTML element.

Args: 1

element HTML token element

returns string name of the HTML element

Markdown

Functions for processing and converting Markdown documents

reader//do-markdown

Processes Markdown using a streaming approach with section handlers.

Args: 2

reader File reader or similar object to read markdown from

handlers Block containing section handlers (h1, h2, paragraph, code, etc.)

returns the result of processing the markdown document

markdown->html

Converts Markdown text to HTML.

Args: 1

text Markdown source string

returns string containing HTML

markdown->html "# Title" |contains "<h"
; returns true
markdown->html 123
; correctly causes error:
; `markdown->html`: first argument must be: String. 

markdown

Creates a Markdown value from a string.

Args: 1

text String or Markdown value

returns Markdown value

markdown "Hello" |type?
; returns markdown

text

Gets the raw markdown text from a Markdown value.

Args: 1

md Markdown value

returns raw markdown text as string

markdown "Hello" |text
; returns "Hello"

to-html

Converts a Markdown value to HTML.

Args: 1

md Markdown value

returns string containing HTML

markdown "# T" |to-html |contains "<h"
; returns true

headings

Extracts all headings from markdown text as a list of strings.

Args: 1

md Markdown value

returns list of strings with headings

markdown "# A
## B" |headings |type?
; returns list

paragraphs

Extracts all paragraphs from markdown text as a list of strings.

Args: 1

md Markdown value

returns list of strings with paragraphs

markdown "Hello

World" |paragraphs |type?
; returns list

links

Extracts all links from markdown text as a list of dictionaries with 'text' and 'url' keys.

Args: 1

md Markdown value

returns list of dicts with 'text and 'url keys

markdown "[rye](https://ryelang.org)" |links |type?
; returns list

Conversion

Functions for converting between different types and kinds

convert

Converts value from one kind to another based on a conversion specification block.

Args: 2

value Dict or RyeCtx to convert

spec Block specifying the conversion rules

returns converted value based on the specification

converter

Registers a converter between two kinds of objects.

Args: 3

source-kind Kind to convert from

target-kind Kind to convert to

spec Block specifying conversion rules

returns the target kind with the converter registered

Default

decode\hex-string

Decodes a hexadecimal string to a UTF-8 string.

Args: 1

pure

hex-string hexadecimal string to decode

returns string containing the decoded bytes as a UTF-8 string

"48656c6c6f20576f726c64" |decode\hex-string
; returns "Hello World"
"48656c6c6f20576f726c64" |decode\hex-string |type?
; returns string
"invalid" |decode\hex-string |disarm |type?
; returns error

encode\hex-string

Encodes a string to its hexadecimal representation.

Args: 1

pure

string string to encode

returns hexadecimal string representation

"Hello World" |encode\hex-string
; returns "48656c6c6f20576f726c64"
"Hello World" |encode\hex-string |type?
; returns string
"" |encode\hex-string
; returns ""

clean\hex-string

Removes all whitespace from a hex string and ensures even length.

Args: 1

pure

hex-string raw hex string with possible whitespace

returns cleaned hex string with whitespace removed and even length

"  70 44 01 4b  
  55 50 4e 51  " |clean\hex-string
; returns "7044014b55504e51"
"70 44 01 4b	55 50 4e 51
52 0a" |clean\hex-string
; returns "7044014b55504e51520a"
"7044014b55504e51520a" |clean\hex-string
; returns "7044014b55504e51520a"

charmap\windows-1250

Returns the Windows-1250 (CP1250) character encoding for Central European languages.

pure

none

returns Windows-1250 encoding as a native value

charmap\windows-1250 |type?
; returns native
charmap\windows-1250 |kind?
; returns charmap-encoding

charmap\iso-8859-1

Returns the ISO-8859-1 (Latin-1) character encoding.

pure

none

returns ISO-8859-1 encoding as a native value

charmap\iso-8859-1 |type?
; returns native
charmap\iso-8859-1 |kind?
; returns charmap-encoding

charmap\iso-8859-2

Returns the ISO-8859-2 (Latin-2) character encoding for Central European languages.

pure

none

returns ISO-8859-2 encoding as a native value

charmap\iso-8859-2 |type?
; returns native
charmap\iso-8859-2 |kind?
; returns charmap-encoding

charmap\windows-1252

Returns the Windows-1252 character encoding for Western European languages.

pure

none

returns Windows-1252 encoding as a native value

charmap\windows-1252 |type?
; returns native
charmap\windows-1252 |kind?
; returns charmap-encoding

charmap\code-page-437

Returns the Code Page 437 (DOS/IBM PC) character encoding.

pure

none

returns Code Page 437 encoding as a native value

charmap\code-page-437 |type?
; returns native
charmap\code-page-437 |kind?
; returns charmap-encoding

charmap\code-page-850

Returns the Code Page 850 (DOS Latin-1) character encoding.

pure

none

returns Code Page 850 encoding as a native value

charmap\code-page-850 |type?
; returns native
charmap\code-page-850 |kind?
; returns charmap-encoding

charmap\koi8r

Returns the KOI8-R character encoding for Russian.

pure

none

returns KOI8-R encoding as a native value

charmap\koi8r |type?
; returns native
charmap\koi8r |kind?
; returns charmap-encoding

charmap-encoding//Decoder

Creates a new text decoder for the given character encoding.

Args: 1

pure

encoding charmap encoding as a native value

returns text decoder as a native value

charmap\windows-1250 |Decoder |type?
; returns native
charmap\windows-1250 |Decoder |kind?
; returns text-decoder

charmap-encoding//Encoder

Creates a new text encoder for the given character encoding.

Args: 1

pure

encoding charmap encoding as a native value

returns text encoder as a native value

charmap\windows-1250 |Encoder |type?
; returns native
charmap\windows-1250 |Encoder |kind?
; returns text-encoder

text-decoder//Decode

Decodes a string using the given text decoder.

Args: 2

pure

decoder text decoder as a native value

input string to decode

returns decoded string

charmap\windows-1250 |Decoder |Decode "Hello"
; returns "Hello"
charmap\windows-1250 |Decoder |Decode ""
; returns ""

text-encoder//Encode

Encodes a string using the given text encoder.

Args: 2

pure

encoder text encoder as a native value

input string to encode

returns encoded string

charmap\windows-1250 |Encoder |Encode "Hello"
; returns "Hello"
charmap\windows-1250 |Encoder |Encode "Plačilo" |encode\hex-string
; returns "506c61e8696c6f"

is-space

Checks if a single character string is a whitespace character.

Args: 1

pure

input string to check (should be single character)

returns boolean true if character is whitespace, false otherwise

" " |is-space
; returns true
"	" |is-space
; returns true
"
" |is-space
; returns true
"
" |is-space
; returns true
"a" |is-space
; returns false
"A" |is-space
; returns false
"1" |is-space
; returns false

remove-all-space

Removes all whitespace characters from a string.

Args: 1

pure

input string to process

returns string with all whitespace characters removed

"Hello World" |remove-all-space
; returns "HelloWorld"
"  Hello  World  " |remove-all-space
; returns "HelloWorld"
"	
 Hello 
 World 	" |remove-all-space
; returns "HelloWorld"
"" |remove-all-space
; returns ""

Structures

Functions for converting between Rye and Go data structures

dict->struct

Converts a Rye Dict to a Go struct using reflection. Takes a Dict and a Native containing a pointer to a struct.

Args: 2

dict Rye Dict containing field names and values

struct-ptr Native containing a pointer to a Go struct

returns native containing the populated struct

dict->new-struct

Creates a new Go struct type at runtime based on a Rye Dict and populates it with values. Takes a Dict and a string for the struct name.

Args: 2

dict Rye Dict containing field names and values

name String name for the new struct type

returns native containing the dynamically created struct

struct->dict

Converts a Go struct to a Rye Dict using reflection. Takes a Native containing a struct or pointer to a struct.

Args: 1

struct Native containing a Go struct or pointer to struct

returns dict with struct field names as keys and field values as values