Skip to content

Commit 5c036b4

Browse files
committed
added str_wrap
1 parent 28753bb commit 5c036b4

File tree

3 files changed

+84
-30
lines changed

3 files changed

+84
-30
lines changed

src/TidierStrings.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export
99
# concatenation
1010
str_c, str_flatten, str_flatten_comma,
1111
# characters
12-
str_dup, str_length, str_width, str_trim, str_squish,
12+
str_dup, str_length, str_width, str_trim, str_squish, str_wrap,
1313
# locale
1414
str_equal, str_to_upper, str_to_lower, str_to_title, str_to_sentence, str_unique,
1515
# other

src/characters.jl

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,33 @@
22
$docstring_str_dup
33
"""
44
function str_dup(s::AbstractString, times::Integer)
5-
if ismissing(s)
6-
return (s)
7-
end
8-
95
return repeat(s, times)
106
end
117

128
"""
139
$docstring_str_length
1410
"""
1511
function str_length(s::AbstractString)
16-
if ismissing(s)
17-
return (s)
18-
end
19-
2012
return length(s)
2113
end
2214

2315
"""
2416
$docstring_str_width
2517
"""
26-
function str_width(s::AbstractString)
27-
if ismissing(s)
28-
return (s)
29-
end
30-
31-
return textwidth(s)
18+
function str_width(s::AbstractString)::Integer
19+
return Base.textwidth(s)
3220
end
3321

3422
"""
3523
$docstring_str_trim
3624
"""
37-
function str_trim(s::AbstractString, side::String="both")
38-
if ismissing(s)
39-
return (s)
40-
end
41-
25+
function str_trim(s::AbstractString, side::String="both")::String
4226
if side == "both"
43-
return strip(s)
27+
return Base.strip(s)
4428
elseif side == "left"
45-
return lstrip(s)
29+
return Base.lstrip(s)
4630
elseif side == "right"
47-
return rstrip(s)
31+
return Base.rstrip(s)
4832
else
4933
throw(ArgumentError("side must be one of 'both', 'left', or 'right'"))
5034
end
@@ -53,12 +37,53 @@ end
5337
"""
5438
$docstring_str_squish
5539
"""
56-
function str_squish(column)
57-
if ismissing(column)
58-
return (column)
40+
function str_squish(column)::String
41+
squished::String = Base.strip(column)
42+
43+
return Base.replace(squished, r"\s+" => Base.s" ")
44+
end
45+
46+
"""
47+
$docstring_str_wrap
48+
"""
49+
function str_wrap(
50+
string::AbstractString;
51+
width::Integer=80,
52+
indent::Integer=0,
53+
exdent::Integer=0,
54+
whitespace_only::Bool=true)::String
55+
56+
width::Integer = Base.max(width, 1)
57+
58+
split_pattern::Regex = r"\s+"
59+
if whitespace_only
60+
split_pattern = r"\s+"
61+
else
62+
split_pattern = r"\W+"
5963
end
60-
# Remove leading and trailing white spaces
61-
squished = strip(column)
62-
# Replace any sequence of whitespace characters with a single space
63-
return replace(squished, r"\s+" => s" ")
64+
65+
words::Vector{SubString{String}} = Base.split(string, split_pattern)
66+
67+
indent_str::String = " "^indent
68+
exdent_str::String = " "^exdent
69+
70+
lines::Vector{String} = []
71+
current_line::String = indent_str
72+
73+
for word in words
74+
if Base.length(current_line) + Base.length(word) + 1 > width
75+
Base.push!(lines, current_line)
76+
current_line = exdent_str * word
77+
else
78+
if current_line == indent_str
79+
current_line *= word
80+
else
81+
current_line *= " " * word
82+
end
83+
end
84+
end
85+
86+
Base.push!(lines, current_line)
87+
88+
return Base.join(lines, "\n")
6489
end

src/docstrings.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,4 +740,33 @@ Examples
740740
julia> str_c(["apple", "banana", "pear", "pineapple"])
741741
"applebananapearpineapple"
742742
```
743+
"""
744+
745+
const docstring_str_wrap =
746+
"""
747+
str_wrap(string::AbstractString; width::Integer=80, indent::Integer=0, exdent::Integer=0, whitespace_only::Bool=true)::String
748+
749+
Wraps a string into multiple lines.
750+
751+
Arguments
752+
- `string`: Input string.
753+
- `width`: The maximum width of each line. Default is 80.
754+
- `indent`: The number of spaces to indent each line. Default is 0.
755+
- `exdent`: The number of spaces to exdent each line. Default is 0.
756+
- `whitespace_only`: Whether to only wrap on whitespace. Default is true.
757+
758+
Returns
759+
The wrapped string.
760+
761+
Examples
762+
```jldoctest
763+
julia> str_wrap("This is an example text that should be wrapped based on the given width and breaking rules.", width=20, whitespace_only=true)
764+
"This is an example\ntext that should be\nwrapped based on the\ngiven width and\nbreaking rules."
765+
766+
julia> str_wrap("This is an example text that should be wrapped based on the given width and breaking rules.", width=20, whitespace_only=false)
767+
"This is an example\ntext that should be\nwrapped based on\nthe given width\nand breaking\nrules."
768+
769+
julia> str_wrap("This is an example text that should be wrapped based on the given width and breaking rules.", width=20, indent=4, whitespace_only=true)
770+
" This is an\n example text\n that should\n be wrapped\n based on the\n given width\n and breaking\n rules."
771+
```
743772
"""

0 commit comments

Comments
 (0)