AngelScript
string object

Path: /sdk/add_on/scriptstdstring/

This add-on registers the std::string type as-is with AngelScript. This gives perfect compatibility with C++ functions that use std::string in parameters or as return type.

A potential drawback is that the std::string type is a value type, thus may increase the number of copies taken when string values are being passed around in the script code. However, this is most likely only a problem for scripts that perform a lot of string operations.

Register the type with RegisterStdString(asIScriptEngine*). Register the optional split method and global join function with RegisterStdStringUtils(asIScriptEngine*). The optional functions require that the array template object has been registered first.

Compile the add-on with the pre-processor define AS_USE_STLNAMES=1 to register the methods with the same names as used by C++ STL where the methods have the same significance. Not all methods from STL is implemented in the add-on, but many of the most frequent once are so a port from script to C++ and vice versa might be easier if STL names are used.

A string pool has been implemented to improve the performance of std strings when the scripts use a lot of string literals. The string pool is not threadsafe though, nor does it work well if you use multiple script engine instances, so by default it is turned off. To use it you need to compile the add-on with the pre-processor define AS_USE_STRINGPOOL=1.

Public C++ interface

Refer to the std::string implementation for your compiler.

Public script interface

  class string
  {
    // Constructors
    string();
    string(const string &in);
    // Property accessor for getting and setting the length
    uint length { get const; set; }
    // Methods for getting and setting the length
    uint length() const;
    void resize(uint);
    // Returns true if the string is empty
    bool isEmpty() const;
    // Assignment and concatenation
    string &opAssign(const string &in other);
    string &opAddAssign(const string &in other);
    string  opAdd(const string &in right) const;
    // Access individual characters
    uint8       &opIndex(uint);
    const uint8 &opIndex(uint) const;
    // Comparison operators
    bool opEquals(const string &in right) const;
    int  opCmp(const string &in right) const;
    // Substring
    string substr(uint start = 0, int count = -1) const;
    array<string>@ split(const string &in delimiter) const;
    // Search
    int findFirst(const string &in str, uint start = 0) const;
    int findLast(const string &in str, int start = -1) const;
    // Automatic conversion from primitive types to string type
    string &opAssign(double val);
    string &opAddAssign(double val);
    string  opAdd(double val) const;
    string  opAdd_r(double val) const;
    string &opAssign(int val);
    string &opAddAssign(int val);
    string  opAdd(int val) const;
    string  opAdd_r(int val) const;
    string &opAssign(uint val);
    string &opAddAssign(uint val);
    string  opAdd(uint val) const;
    string  opAdd_r(uint val) const;
    string &opAssign(bool val);
    string &opAddAssign(bool val);
    string  opAdd(bool val) const;
    string  opAdd_r(bool val) const;
  }
  // Takes an array of strings and joins them into one string separated by the specified delimiter
  string join(const array<string> &in arr, const string &in delimiter);
  // Formatting numbers into strings
  // The options should be informed as characters in a string
  //  l = left justify
  //  0 = pad with zeroes
  //  + = always include the sign, even if positive
  //    = add a space in case of positive number
  //  h = hexadecimal integer small letters
  //  H = hexadecimal integer capital letters
  //  e = exponent character with small e
  //  E = exponent character with capital E
  string formatInt(int64 val, const string &in options, uint width = 0);
  string formatFloat(double val, const string &in options, uint width = 0, uint precision = 0);
  // Parsing numbers from strings
  int64  parseInt(const string &in, uint base = 10, uint &out byteCount = 0);
  double parseFloat(const string &in, uint &out byteCount = 0);