Strings

Files CCore/inc/String.h CCore/src/String.cpp

The class String implements immutable, atomically reference counted string. The implementation of the class is a very simple, because it uses AtomicRefArray. The String is not zero-terminated.


class String
 {
   AtomicRefArray<char> data; 
   
  public: 
  
   String() noexcept {}
   
   String(NothingType) noexcept {}

   String(StrLen str) : data(DoCopy(str.len),str.ptr) {}
   
   template <class Builder>
   String(DoBuildType,Builder builder) : data(DoBuild,builder) {}
   
   ~String() {}
   
   // range access
   
   const char * getPtr() const { return data.getPtr(); }
   
   const char * getPtr_const() const { return data.getPtr(); }
   
   ulen getLen() const { return data.getLen(); }
   
   // print object
   
   using PrintOptType = StrPrintOpt ;
   
   template <class P>
   void print(P &obj,PrintOptType opt) const;
   
   // swap/move objects
   
   void objSwap(String &obj);
    
   explicit String(ToMoveCtor<String> obj); 
 };

String can be constructed from a character range. It also has a custom Builder constructor.

String is copyable, swappable and movable. These operations are efficient.

String implements the read-only Range Access Interface.

String is printable, usual string print options are accepted.

PrintString

PrintString can be used to print into "string". Printed characters are accumulated in some internal memory storage, once you have finished, you can build a string using the method close().


class PrintString : public PrintBase
 {
   ....

  public: 
  
   static const ulen DefaultFrameLen = 1_KByte ;
  
   explicit PrintString(ulen max_len=MaxULen,ulen frame_len=DefaultFrameLen);
   
   ~PrintString() { cleanup(); }
   
   String close();
   
   void cleanup();
 };

In a constructor call you may specify the maximum string length and the frame length (printed characters are stored in a list of frames of the given length).

cleanup() erases all printed characters.

close() creates a string from printed characters. You may continue printing after a call of this method, extending the printed sequence.

Stringf() and StringCat()

Stringf() and StringCat() are convenient functions to create a string, using printing:


template <class ... TT>
String Stringf(const char *format,const TT & ... tt);
 
template <class ... TT>
String StringCat(const TT & ... tt);

They are implemented using PrintString. Stringf() does usual formatted printing and returns the result as a String. StringCat() concatenates printed objects.

Concatenation of string-like objects

You can add an object to the string, if the object is castable to const char *, StrLen or the Range_const() is applied and gives the PtrLen<const char>.


template <class T>
String operator + (const String &a,const T &t) { return StringSum(a,t); }

To sum more than two such objects, use the function StringSum().


template <class ... TT>
String StringSum(const TT & ... tt);