Skip to content

Borland String Handling

DryPerspective edited this page Sep 22, 2023 · 5 revisions

As an extra feature, the library contains a header borland/borland_strings.h for handling the Borland UnicodeString and AnsiString types. It provides the AnsiString_view and UnicodeString_view classes, and overloads for dp::data() for UnicodeString and AnsiString, and those views.

The two view classes have identical interfaces to the string_view class template found in cpp98/string_view.h, with the exception that in addition to being constructible from anything their corresponding string_view type is constructible from, they can also be constructed from AnsiString and UnicodeString respectively. Note that while Embarcadero are happy to bend the rules on character encoding and create a UnicodeString from a plain old char literal, strict typing in standard C++ means that I am unable to do the same, so only widestring literals (L"foo") can be used when constructing a UnicodeString_view.

It is worth noting that in many imeplementations, these string types are reference-counted, CoW types; and that the rest of the C++Builder ecosystem may not be equipped to handle views without converting them back to their string types first. Care should be taken when using these to ensure that their use does not lead to more allocation than it avoids.

This header will cause a preprocessor error if included while __BORLANDC__ is not defined, and should be the only platform-specific code in this project, with the exception of some switches in cpp98/type_traits.h to selectively disable certain traits which the Borland compiler cannot process.

Contents

AnsiString_view A dp::string_view which also works for Borland AnsiString types
UnicodeString_view A dp::wstring_view which also works for Borland UnicodeString types
data(AnsiString)
data(UnicodeString)
Overloads of dp::data for AnsiStrings and UnicodeStrings
data(AnsiString_view)
data(UnicodeString_view)
Overloads of dp::data for the views included in this header
empty(AnsiString)
empty(UnicodeString)
Overloads of dp::empty for AnsiStrings and UnicodeStrings
size(AnsiString)
size(UnicodeString)
Overloads of dp::size for AnsiStrings and UnicodeStrings
ssize(AnsiString)
ssize(UnicodeString)
Overloads of dp::ssize for AnsiStrings and UnicodeStrings

Sample code

#include "borland/borland_views.h"

void func(dp::UnicodeString_view str){
    if(str.length() > 10) str.remove_suffix(str.length() - 10);
    ShowMessage(str);
}

void CallingCode(){
   UnicodeString us = L"Hello world";
   func(us);
   func(L"Also works for literals");
   std::wstring ws = "And std::strings";
   func(ws);
}

Clone this wiki locally