Skip to content

Commit

Permalink
Add docstring to String (#43943)
Browse files Browse the repository at this point in the history
The type `String` itself did not have a docstring, instead the type is
documented through two of its constructors.
Add docstring to `String`, and remove redundant information from the
constructor docstrings.
In particular, mention that `String`s, while they are interpreted as being
UTF8 encoded, do not assume strings are valid UTF8, and may be arbitrary
byte sequences.
  • Loading branch information
jakobnissen committed Feb 19, 2022
1 parent 5bd0545 commit 749a658
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions base/strings/string.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,28 @@ const ByteArray = Union{CodeUnits{UInt8,String}, Vector{UInt8},Vector{Int8}, Fas

@inline between(b::T, lo::T, hi::T) where {T<:Integer} = (lo b) & (b hi)

"""
String <: AbstractString
The default string type in Julia, used by e.g. string literals.
`String`s are immutable sequences of `Char`s. A `String` is stored internally as
a contiguous byte array, and while they are interpreted as being UTF-8 encoded,
they can be composed of any byte sequence. Use [`isvalid`](@ref) to validate
that the underlying byte sequence is valid as UTF-8.
"""
String

## constructors and conversions ##

# String constructor docstring from boot.jl, workaround for #16730
# and the unavailability of @doc in boot.jl context.
"""
String(v::AbstractVector{UInt8})
Create a new `String` object from a byte vector `v` containing UTF-8 encoded
characters. If `v` is `Vector{UInt8}` it will be truncated to zero length and
future modification of `v` cannot affect the contents of the resulting string.
Create a new `String` object using the data buffer from byte vector `v`.
If `v` is a `Vector{UInt8}` it will be truncated to zero length and future
modification of `v` cannot affect the contents of the resulting string.
To avoid truncation of `Vector{UInt8}` data, use `String(copy(v))`; for other
`AbstractVector` types, `String(v)` already makes a copy.
Expand Down Expand Up @@ -78,8 +90,7 @@ end
"""
String(s::AbstractString)
Convert a string to a contiguous byte array representation encoded as UTF-8 bytes.
This representation is often appropriate for passing strings to C.
Create a new `String` from an existing `AbstractString`.
"""
String(s::AbstractString) = print_to_string(s)
@pure String(s::Symbol) = unsafe_string(unsafe_convert(Ptr{UInt8}, s))
Expand Down

0 comments on commit 749a658

Please sign in to comment.