core :: Buffer :: capitalize
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"
# 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
lib/core/text/abstract_text.nit:1629,2--1684,4