/* vim: set ts=4 sw=4 tw=99 et:
 *
 * Copyright (C) 2012 David Anderson
 *
 * This file is part of JITCraft.
 *
 * JITCraft is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 * 
 * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * JITCraft. If not, see http://www.gnu.org/licenses/.
 */
#ifndef _include_jitcraft_string_h_
#define _include_jitcraft_string_h_

#include "Heap.h"

namespace ke {

// A fixed-length string containing UTF-8 characters.
class String : public Object
{
    uint32 length_;
    
  public:
    static String *New(Zone *zone, const char *from, Heap::Tenure tenure = Heap::Tenure_Default);
    static String *New(Zone *zone, const char *from, size_t length, Heap::Tenure tenure = Heap::Tenure_Default);

    unsigned length() {
        return length_;
    }
    char *chars() {
        return reinterpret_cast<char *>(this + 1);
    }

    static size_t SizeFor(unsigned length) {
        return Align(sizeof(String) + sizeof(char) * (length + 1), kObjectAlignment);
    }

    static inline String *cast(Object *obj) {
        assert(!obj || obj->is(MapKind_String));
        return reinterpret_cast<String *>(obj);
    }
};

}

#endif // _include_jitcraft_string_h_
