string

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 7 Imported by: 0

README

string

string provides constants and functions to manipulate strings, it's intended to be a drop-in subset of Python's string module for Starlark.

Functions

length(obj) int

Returns the length of the object; for string, it returns the number of Unicode code points, instead of bytes like len().

Parameters
name type description
obj string The object whose length is to be calculated
Examples

String

Calculate the length of a CJK string.

load("string", "length")
s = "你好"
print(length(s), len(s))
# Output: 2 6

Misc

Calculate the length of a list, set and map.

load("string", "length")
print(length([1, 2, 3]), length(set([1, 2])), length({1: 2}))
# Output: 3 2 1
reverse(str) string

Returns the reversed string of the given value.

Parameters
name type description
str string A string that is to be reversed
Examples

Basic

Reverse a string.

load("string", "reverse")
s = "123我爱你"
print(reverse(s))
# Output: 你爱我321
escape(str) string

Converts the characters "&", "<", ">", '"' and "'" in string to their corresponding HTML entities.

Parameters
name type description
str string A string which is to be HTML escaped
Examples

Basic

Escape a string.

load("string", "escape")
s = "Hello<World>"
print(escape(s))
# Output: Hello&lt;World&gt;
unescape(str) string

Converts the HTML entities in a string back to their corresponding characters.

Parameters
name type description
str string A HTML escaped string
Examples

Basic

Unescape a string.

load("string", "unescape")
s = "You&amp;Me"
print(unescape(s))
# Output: "You&Me"
quote(str) string

Returns a shell-escaped version of the string str. This returns a string that can safely be used as one token in a shell command line.

Parameters
name type description
str string A string which is to be quoted
Examples

Basic

Quote a string.

load("string", "quote")
s = "Hello World"
print(quote(s))
# Output: "Hello World"
unquote(str) string

Returns a shell-unescaped version of the string str. This returns a string that was used as one token in a shell command line.

Parameters
name type description
str string A string which is to be unquoted
Examples

Basic

Unquote a string.

load("string", "unquote")
s = '"Hello\tWorld"'
print(unquote(s))
World

Documentation

Overview

Package string defines functions that manipulate strings, it's intended to be a drop-in subset of Python's string module for Starlark. See https://docs.python.org/3/library/string.html and https://github.com/python/cpython/blob/main/Lib/string.py for reference.

Index

Constants

View Source
const ModuleName = "string"

ModuleName defines the expected name for this Module when used in starlark's load() function, eg: load('string', 'length')

Variables

This section is empty.

Functions

func LoadModule

func LoadModule() (starlark.StringDict, error)

LoadModule loads the string module. It is concurrency-safe and idempotent.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL