1. 12 Dec, 2018 2 commits
    • Brad King's avatar
      String: Add str_if_stable() as a const alternative to str() · 2d68b2c5
      Brad King authored
      The `str()` method must be non-const because it may need to internally
      mutate the representation of the string in order to have an owned
      `std::string` instance holding the exact string (not a superstring).
      This is inconvenient in contexts where we can ensure that no mutation
      is needed to get a `std::string const&`.
      
      Add a `str_if_stable() const` method that returns `std::string const*`
      so we can return `nullptr` if if mutation would be necessary to get a
      `std::string const&`.  Add supporting `is_stable() const` and
      `stabilize()` methods to check and enforce stable availability of
      `std::string const&`.  These can be used to create `String const`
      instances from which we can still get a `std::string const&` via
      `*str_if_stable()` by maintaining the stability invariant at runtime.
      2d68b2c5
    • Brad King's avatar
      String: Add a custom string type · ff69763c
      Brad King authored
      Create a `cm::String` type that holds a view of a string buffer and
      optionally shares ownership of the buffer.  Instances can either
      borrow longer-lived storage (e.g. static storage of string literals)
      or internally own a `std::string` instance.  In the latter case,
      share ownership with copies and substrings.  Allocate a new internal
      string only on operations that require mutation.
      
      This will allow us to recover string sharing semantics that we
      used to get from C++98 std::string copy-on-write implementations.
      Such implementations are not allowed by C++11 so code our own in
      a custom string type instead.
      ff69763c