class String
Defined at line 31 of file ../../zircon/kernel/lib/efi/string/include/efi/string/string.h
UEFI string class.
UEFI uses UCS-2 (a subset of UTF-16) as its string representation, but many libraries and
applications use UTF-8. This class helps easily convert between the two formats to bridge
these gaps.
Additionally, std::string is not available in our EFI environment so this class helps smooth
over some of the pain points of memory management using other storage classes.
This class stores both the UTF-8 and UCS-2 representation of the string on the heap, so
should be avoided unless you really do need to convert between both formats. There's also
currently no support for holding one format as a view, so using this for literals will be
inefficient as it will allocate heap memory for a duplication of the string literal.
Note: this implementation currently uses true UTF-16 rather than UCS-2, which may rarely result
in certain strings being rejected or incorrectly parsed by UEFI.
Public Methods
void String ()
Users should check IsValid() after construction to ensure the conversion was successful. On
failure both representations will be erased (i.e. it will not have one valid format and the
other invalid).
Constructors taking in string_view expect unterminated input, as is generally standard when
working with string_views; a null terminator will always be appended to the internal storage.
Constructors taking in fbl::Vector
<
> expect terminated input and do *not* append terminators
internally, since in this case it's expected that the entire null-terminated string resides
in the allocated vector. In the spirit of fbl::Vector's care with memory allocation, we do not
provide a constructor to implicitly copy a vector; if needed, the caller must copy the vector
themselves and move in the copy.
Defined at line 45 of file ../../zircon/kernel/lib/efi/string/include/efi/string/string.h
void String (String && other)
Defined at line 57 of file ../../zircon/kernel/lib/efi/string/include/efi/string/string.h
String & operator= (String && other)
Defined at line 58 of file ../../zircon/kernel/lib/efi/string/include/efi/string/string.h
bool IsValid ()
Returns true if the string is valid in both UTF-8 and UCS-2.
Defined at line 61 of file ../../zircon/kernel/lib/efi/string/include/efi/string/string.h
std::string_view operator basic_string_view ()
Implicit conversion to string_view.
If IsValid() is true, the returned string_view will not include a trailing terminator, but
the underlying storage will, making it safe to printf/strcmp/etc.
If IsValid() is false, the returned string_view will point to null and be 0-length.
Defined at line 69 of file ../../zircon/kernel/lib/efi/string/include/efi/string/string.h
void String (std::string_view contents)
Defined at line 75 of file ../../zircon/kernel/lib/efi/string/string.cc
void String (std::string_view contents)
Defined at line 75 of file ../../zircon/kernel/lib/efi/string/string.cc
void String (std::u16string_view contents)
Defined at line 84 of file ../../zircon/kernel/lib/efi/string/string.cc
void String (std::u16string_view contents)
Defined at line 84 of file ../../zircon/kernel/lib/efi/string/string.cc
void String (fbl::Vector<char> && contents)
Defined at line 92 of file ../../zircon/kernel/lib/efi/string/string.cc
void String (fbl::Vector<char16_t> && contents)
Defined at line 96 of file ../../zircon/kernel/lib/efi/string/string.cc
void String (const String & other)
Copyable and movable.
We do allow implicit copies of the full String object, which is a deep copy and will
allocate duplicates of the underlying strings.
Defined at line 100 of file ../../zircon/kernel/lib/efi/string/string.cc
String & operator= (const String & other)
Defined at line 102 of file ../../zircon/kernel/lib/efi/string/string.cc
const char * c_str ()
Defined at line 125 of file ../../zircon/kernel/lib/efi/string/string.cc
std::tuple<fbl::Vector<char>, fbl::Vector<char16_t>> TakeData ()
Moves ovnership of UTF8 and UCS2 vectors to the caller.
Object is reset to empty state.
Use example:
```
auto [name_utf8, name_ucs2] = variable_id.name.TakeData();
```
Defined at line 120 of file ../../zircon/kernel/lib/efi/string/string.cc
fbl::Vector<char> ToUtf8 (std::u16string_view contents)
Converts between UTF-8 and UCS-2.
Standalone conversion functions for cases where we don't need to track both formats
persistently, but just need a one-time allocation and conversion.
As with construction, the string_view input is not expected to be null-terminated, and a
terminator will be automatically appended in the returned storage.
On conversion failure, the returned vector will be empty.
Defined at line 108 of file ../../zircon/kernel/lib/efi/string/string.cc
fbl::Vector<char> ToUtf8 (std::u16string_view contents)
Converts between UTF-8 and UCS-2.
Standalone conversion functions for cases where we don't need to track both formats
persistently, but just need a one-time allocation and conversion.
As with construction, the string_view input is not expected to be null-terminated, and a
terminator will be automatically appended in the returned storage.
On conversion failure, the returned vector will be empty.
Defined at line 108 of file ../../zircon/kernel/lib/efi/string/string.cc
fbl::Vector<char16_t> ToUtf16 (std::string_view contents)
Defined at line 114 of file ../../zircon/kernel/lib/efi/string/string.cc
fbl::Vector<char16_t> ToUtf16 (std::string_view contents)
Defined at line 114 of file ../../zircon/kernel/lib/efi/string/string.cc