Code View

fson / source / fson-1.1.0.0 / libs / internal / sdk / fson / String.hh
// SPDX-License-Identifier: MIT
#pragma once

#include <memory>
#include <string>

#include "Value.hh"

namespace fedem
{
  namespace fson
  {
    // ─────────────────────────────────────────────────────────────────────
    // String
    //
    // Stores the DECODED character sequence (escape sequences resolved).
    // The writer re-encodes per the string grammar
    // (docs/notation/00040-string.rrd) on output.
    //
    // Form records how the value was written, for round-trip:
    //   Quoted    — a "…" or '…' string (default; the writer picks the
    //               quote style and escapes as needed).
    //   Multiline — a `|`-block: one `|`-line per source line, each adding a
    //               trailing '\n' to the value (a line ending with '\' joins
    //               the next without the newline). On output the writer
    //               reconstructs a canonical block — one `|`-line per
    //               '\n'-separated segment — at the value's indentation.
    //               Presentation (indent, any '\'-continuation the author
    //               used) is normalised; the decoded content is preserved
    //               exactly. A Multiline value whose content has no trailing
    //               '\n' cannot be represented as a block and is written
    //               quoted instead (the writer falls back automatically).
    // ─────────────────────────────────────────────────────────────────────
    class String final : public Value
    {
      public:
        enum class Form
        {
          Quoted,
          Multiline
        };

        explicit String( std::string value = std::string( ),
                         Form form = Form::Quoted );

        Type getType( ) const noexcept override;
        std::unique_ptr< Value > clone( ) const override;

        String*       asString( )       noexcept override;
        String const* asString( ) const noexcept override;

        std::string const& getValue( ) const noexcept;
        void setValue( std::string value );

        Form getForm( ) const noexcept;
        void setForm( Form form ) noexcept;

      private:
        std::string value;
        Form        form;
    };

    inline String::String( std::string value, Form form )
    : value( std::move( value ) )
    , form( form )
    {
    }

    inline Value::Type String::getType( ) const noexcept
    {
      return Type::String;
    }

    inline std::unique_ptr< Value > String::clone( ) const
    {
      return std::make_unique< String >( value, form );
    }

    inline String* String::asString( ) noexcept
    {
      return this;
    }

    inline String const* String::asString( ) const noexcept
    {
      return this;
    }

    inline std::string const& String::getValue( ) const noexcept
    {
      return value;
    }

    inline void String::setValue( std::string value )
    {
      this->value = std::move( value );
    }

    inline String::Form String::getForm( ) const noexcept
    {
      return form;
    }

    inline void String::setForm( Form form ) noexcept
    {
      this->form = form;
    }
  }  // end namespace fson
}  // end namespace fedem