core :: Buffer :: append_substring
Appendslength
chars from s
starting at index from
core :: Buffer :: append_substring_impl
Unsafe version ofappend_substring
for performance
core :: Buffer :: capitalize
Capitalizes each word inself
core :: Buffer :: defaultinit
core $ Buffer :: capitalized
Returns a capitalizedself
core $ Buffer :: to_camel_case
Takes a snake caseself
and converts it to camel case
core $ Buffer :: to_snake_case
Takes a camel caseself
and converts it to snake case
serialization :: Serializable :: accept_json_serializer
Refinable service to customize the serialization of this class to JSONserialization :: Serializable :: accept_msgpack_attribute_counter
Hook to customize the behavior of theAttributeCounter
serialization :: Serializable :: accept_msgpack_serializer
Hook to customize the serialization of this class to MessagePackserialization :: Serializable :: add_to_bundle
Called by[]=
to dynamically choose the appropriate method according
core :: Buffer :: append_substring
Appendslength
chars from s
starting at index from
core :: Buffer :: append_substring_impl
Unsafe version ofappend_substring
for performance
core :: Text :: binarydigest_to_bytes
Return aBytes
by reading 0 and 1.
core :: Buffer :: capitalize
Capitalizes each word inself
core :: Text :: capitalized
Returns a capitalizedself
core :: Object :: class_factory
Implementation used byget_class
to create the specific class.
core :: Text :: copy_to_native
Copiesn
bytes from self
at src_offset
into dest
starting at dest_offset
serialization :: Serializable :: core_serialize_to
Actual serialization ofself
to serializer
core :: Text :: decode_base64
Decodes the receiver string to base64 using a custom padding character.core :: Pattern :: defaultinit
core :: Object :: defaultinit
mpi :: Sendable :: defaultinit
core :: Writable :: defaultinit
core :: Text :: defaultinit
core :: Cloneable :: defaultinit
core :: Comparable :: defaultinit
core :: Buffer :: defaultinit
core :: Text :: deserialize_json
Deserialize anullable Object
from this JSON formatted string
core :: Text :: encode_base64
Encodes the receiver string to base64 using a custom padding character.core :: Text :: english_scoring
Scoreself
according to english's letter frequency.
core :: Text :: escape_more_to_c
Escape additionnal characterscore :: Text :: escape_to_c
Escape"
\
'
, trigraphs and non printable characters using the rules of literal C strings and characters
core :: Text :: escape_to_js
Escape the content ofself
to pass to JavaScript code
core :: Text :: escape_to_utf16
Returnsself
with all characters escaped with their UTF-16 representation
core :: Text :: file_extension
Return right-most extension (without the dot)core :: Text :: file_lstat
The status of a file or of a symlink. see POSIX lstat(2).serialization :: Serializable :: from_deserializer
Create an instance of this class from thedeserializer
core :: Text :: from_percent_encoding
Decodeself
from percent (or URL) encoding to a clear string
core :: Text :: from_utf16_digit
Returns a UTF-16 escape valuecore :: Text :: from_utf16_escape
Returns the Unicode char escaped byself
core :: Text :: group_exists
Does the operating system know the group namedself
?
core :: Text :: has_substring
Does self have a substringstr
starting from position pos
?
core :: Text :: hexdigest_to_bytes
Returns a newBytes
instance with the digest as content
core :: Text :: html_escape
Escape the characters<
, >
, &
, "
, '
and /
as HTML/XML entity references.
core :: Text :: http_download
Download the file at URLself
to output_path
with a simple HTTP request
core :: Text :: index_of_from
Gets the index of the first occurence of ´c´ starting from ´pos´core :: Text :: is_numeric
Is this string in a valid numeric format compatible withto_f
?
core :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
core :: Text :: is_whitespace
Is the string non-empty but only made of whitespaces?core :: Text :: last_index_of_from
The index of the last occurrence of an element starting from pos (in reverse order).core :: Text :: levenshtein_distance
Return the Levenshtein distance between two stringscore :: Text :: light_gray
Make the text appear in light gray (or white) in a ANSI/VT100 terminal.serialization :: Serializable :: msgpack_extra_array_items
Hook to request a larger than usual metadata arraycore :: Object :: output_class_name
Display class name on stdout (debug only).core :: Text :: parse_bmfont
Parseself
as an XML BMFont description file
core :: Text :: remove_all
Returns a copy ofself
minus all occurences of pattern
core :: Text :: replace_first
Replace the first occurrence ofpattern
with string
core :: Text :: search_all
Search all occurrences ofpattern
into self.
core :: Pattern :: search_all_in
Search allself
occurrences into s
.
core :: Text :: search_from
Search the first occurence ofpattern
after from
.
core :: Pattern :: search_index_in
Searchself
into s
from a certain position.
core :: Text :: search_last
Search the last occurence of the textt
.
core :: Text :: search_last_up_to
Search the last occurence of the textt
before up_to
.
serialization :: Serializable :: serialize_msgpack
Serializeself
to MessagePack bytes
serialization :: Serializable :: serialize_to
Serializeself
to serializer
serialization :: Serializable :: serialize_to_json
Serializeself
to JSON
core :: Text :: simplify_path
Simplify a file path by remove useless.
, removing //
, and resolving ..
core :: Text :: split_once_on
Splitself
on the first occurence of pattern
core :: Text :: split_with
@deprecated alias forsplit
core :: Text :: strip_extension
Remove the trailingextension
.
core :: Text :: strip_nullable
Strip thenullable
prefix from the type name self
core :: Text :: strip_nullable_and_params
Strip thenullable
prefix and the params from the type name self
core :: Text :: substring_from
Create a substring fromself
beginning at the from
position
core :: Text :: to_camel_case
Takes a snake caseself
and converts it to camel case
core :: Text :: to_cmangle
Mangle a string to be a unique string only made of alphanumeric characters and underscores.core :: Text :: to_percent_encoding
Encodeself
to percent (or URL) encoding
serialization :: Serializable :: to_pretty_json
Serializeself
to plain pretty JSON
core :: Text :: to_program_name
Convert the path (self
) to a program name.
core :: Text :: to_snake_case
Takes a camel caseself
and converts it to snake case
core :: Text :: to_sql_date_string
Format the date represented byself
into an escaped string for SQLite
core :: Text :: unescape_json
Removes JSON-escaping if necessary in a JSON stringcore :: Text :: unescape_nit
Return a string where Nit escape sequences are transformed.core :: Text :: unescape_to_bytes
Return aBytes
instance where Nit escape sequences are transformed.
core :: Text :: user_exists
Does the operating system know the user namedself
?
core :: Writable :: write_to_bytes
Likewrite_to
but return a new Bytes (may be quite large)
core :: Writable :: write_to_file
Likewrite_to
but take care of creating the file
core :: Writable :: write_to_string
Likewrite_to
but return a new String (may be quite large).
serialization :: DirectSerializable
Instances of this class are not delayed and instead serialized immediatelySerializer::serialize
# A mutable sequence of characters.
abstract class Buffer
super Text
# Returns an arbitrary subclass of `Buffer` with default parameters
new is abstract
# Returns an instance of a subclass of `Buffer` with `i` base capacity
new with_cap(i: Int) is abstract
# Returns an instance of a subclass of `Buffer` with `t` as content
new from_text(t: Text) do
var ret = new Buffer.with_cap(t.byte_length)
ret.append t
return ret
end
redef type SELFTYPE: Buffer is fixed
# Copy-On-Write flag
#
# If the `Buffer` was to_s'd, the next in-place altering
# operation will cause the current `Buffer` to be re-allocated.
#
# The flag will then be set at `false`.
protected var written = false
# Modifies the char contained at pos `index`
fun []=(index: Int, item: Char) is abstract
redef fun to_buffer do return clone
# ~~~
# var b = new Buffer
# b.append("Buffer!")
# var c = b.clone
# assert b == c
# ~~~
redef fun clone do
var cln = new Buffer.with_cap(byte_length)
cln.append self
return cln
end
# Adds a char `c` at the end of self
fun add(c: Char) is abstract
# Clears the buffer
#
# ~~~
# var b = new Buffer
# b.append "hello"
# assert not b.is_empty
# b.clear
# assert b.is_empty
# ~~~
fun clear is abstract
# Enlarges the subsequent array containing the chars of self
fun enlarge(cap: Int) is abstract
# Adds the content of text `s` at the end of self
#
# ~~~
# var b = new Buffer
# b.append "hello"
# b.append "world"
# assert b == "helloworld"
# ~~~
fun append(s: Text) is abstract
# `self` is appended in such a way that `self` is repeated `r` times
#
# ~~~
# var b = new Buffer
# b.append "hello"
# b.times 3
# assert b == "hellohellohello"
# ~~~
fun times(r: Int) is abstract
# Reverses itself in-place
#
# ~~~
# var b = new Buffer
# b.append("hello")
# b.reverse
# assert b == "olleh"
# ~~~
fun reverse is abstract
# Changes each lower-case char in `self` by its upper-case variant
#
# ~~~
# var b = new Buffer
# b.append("Hello World!")
# b.upper
# assert b == "HELLO WORLD!"
# ~~~
fun upper is abstract
# Changes each upper-case char in `self` by its lower-case variant
#
# ~~~
# var b = new Buffer
# b.append("Hello World!")
# b.lower
# assert b == "hello world!"
# ~~~
fun lower is abstract
# Capitalizes each word in `self`
#
# Letters that follow a letter are lowercased
# Letters that follow a non-letter are upcased.
#
# If `keep_upper = true`, uppercase letters are not lowercased.
#
# When `src` is specified, this method reads from `src` instead of `self`
# but it still writes the result to the beginning of `self`.
# This requires `self` to have the capacity to receive all of the
# capitalized content of `src`.
#
# SEE: `Char::is_letter` for the definition of a letter.
#
# ~~~
# var b = new FlatBuffer.from("jAVAsCriPt")
# b.capitalize
# assert b == "Javascript"
# b = new FlatBuffer.from("i am root")
# b.capitalize
# assert b == "I Am Root"
# b = new FlatBuffer.from("ab_c -ab0c ab\nc")
# b.capitalize
# assert b == "Ab_C -Ab0C Ab\nC"
#
# b = new FlatBuffer.from("12345")
# b.capitalize(src="foo")
# assert b == "Foo45"
#
# b = new FlatBuffer.from("preserve my ACRONYMS")
# b.capitalize(keep_upper=true)
# assert b == "Preserve My ACRONYMS"
# ~~~
fun capitalize(keep_upper: nullable Bool, src: nullable Text) do
src = src or else self
var length = src.length
if length == 0 then return
keep_upper = keep_upper or else false
var c = src[0].to_upper
self[0] = c
var prev = c
for i in [1 .. length[ do
prev = c
c = src[i]
if prev.is_letter then
if keep_upper then
self[i] = c
else
self[i] = c.to_lower
end
else
self[i] = c.to_upper
end
end
end
# In Buffers, the internal sequence of character is mutable
# Thus, `chars` can be used to modify the buffer.
redef fun chars: Sequence[Char] is abstract
# Appends `length` chars from `s` starting at index `from`
#
# ~~~
# var b = new Buffer
# b.append_substring("abcde", 1, 2)
# assert b == "bc"
# b.append_substring("vwxyz", 2, 3)
# assert b == "bcxyz"
# b.append_substring("ABCDE", 4, 300)
# assert b == "bcxyzE"
# b.append_substring("VWXYZ", 400, 1)
# assert b == "bcxyzE"
# ~~~
fun append_substring(s: Text, from, length: Int) do
if from < 0 then
length += from
from = 0
end
var ln = s.length
if (length + from) > ln then length = ln - from
if length <= 0 then return
append_substring_impl(s, from, length)
end
# Unsafe version of `append_substring` for performance
#
# NOTE: Use only if sure about `from` and `length`, no checks
# or bound recalculation is done
fun append_substring_impl(s: Text, from, length: Int) do
var max = from + length
for i in [from .. max[ do add s[i]
end
redef fun *(i) do
var ret = new Buffer.with_cap(byte_length * i)
for its in [0 .. i[ do ret.append self
return ret
end
redef fun insert_at(s, pos) do
var obuf = new Buffer.with_cap(byte_length + s.byte_length)
obuf.append_substring(self, 0, pos)
obuf.append s
obuf.append_substring(self, pos, length - pos)
return obuf
end
# Inserts `s` at position `pos`
#
# ~~~
# var b = new Buffer
# b.append "美しい世界"
# b.insert(" nit ", 3)
# assert b == "美しい nit 世界"
# ~~~
fun insert(s: Text, pos: Int) is abstract
# Inserts `c` at position `pos`
#
# ~~~
# var b = new Buffer
# b.append "美しい世界"
# b.insert_char(' ', 3)
# assert b == "美しい 世界"
# ~~~
fun insert_char(c: Char, pos: Int) is abstract
# Removes a substring from `self` at position `pos`
#
# NOTE: `length` defaults to 1, expressed in chars
#
# ~~~
# var b = new Buffer
# b.append("美しい 世界")
# b.remove_at(3)
# assert b == "美しい世界"
# b.remove_at(1, 2)
# assert b == "美世界"
# ~~~
fun remove_at(pos: Int, length: nullable Int) is abstract
redef fun reversed do
var ret = clone
ret.reverse
return ret
end
redef fun to_upper do
var ret = clone
ret.upper
return ret
end
redef fun to_lower do
var ret = clone
ret.lower
return ret
end
redef fun to_snake_case do
var ret = clone
ret.snake_case
return ret
end
# Takes a camel case `self` and converts it to snake case
#
# SEE: `to_snake_case`
fun snake_case do
if self.is_lower then return
var prev_is_lower = false
var prev_is_upper = false
var i = 0
while i < length do
var char = chars[i]
if char.is_lower then
prev_is_lower = true
prev_is_upper = false
else if char.is_upper then
if prev_is_lower then
insert_char('_', i)
i += 1
else if prev_is_upper and i + 1 < length and self[i + 1].is_lower then
insert_char('_', i)
i += 1
end
self[i] = char.to_lower
prev_is_lower = false
prev_is_upper = true
else
prev_is_lower = false
prev_is_upper = false
end
i += 1
end
end
redef fun to_camel_case
do
var new_str = clone
new_str.camel_case
return new_str
end
# Takes a snake case `self` and converts it to camel case
#
# SEE: `to_camel_case`
fun camel_case do
if is_upper then return
var underscore_count = 0
var pos = 1
while pos < length do
var char = self[pos]
if char == '_' then
underscore_count += 1
else if underscore_count > 0 then
pos -= underscore_count
remove_at(pos, underscore_count)
self[pos] = char.to_upper
underscore_count = 0
end
pos += 1
end
if underscore_count > 0 then remove_at(pos - underscore_count - 1, underscore_count)
end
redef fun capitalized(keep_upper) do
if length == 0 then return self
var buf = new Buffer.with_cap(byte_length)
buf.capitalize(keep_upper=keep_upper, src=self)
return buf
end
end
lib/core/text/abstract_text.nit:1518,1--1866,3
redef class Buffer
redef new do return new FlatBuffer
redef new with_cap(i) do return new FlatBuffer.with_capacity(i)
end
lib/core/text/flat.nit:859,1--863,3