src/Strings.h
author Michael Busby <prodigysim@gmail.com>
Mon Jun 10 01:50:18 2013 +0100 (2013-06-10)
changeset 264 70b8f318c379
parent 263 ba85a47ee414
permissions -rw-r--r--
Parse assignment RTL instead of LTR, fixes multiple assignment (bug 5420).
asherkin@263
     1
/* vim: set ts=4 sw=4 tw=99 et:
asherkin@263
     2
 *
asherkin@263
     3
 * Copyright (C) 2012 David Anderson
asherkin@263
     4
 *
asherkin@263
     5
 * This file is part of JITCraft.
asherkin@263
     6
 *
asherkin@263
     7
 * JITCraft is free software: you can redistribute it and/or modify it under
asherkin@263
     8
 * the terms of the GNU General Public License as published by the Free
asherkin@263
     9
 * Software Foundation, either version 3 of the License, or (at your option)
asherkin@263
    10
 * any later version.
asherkin@263
    11
 * 
asherkin@263
    12
 * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
asherkin@263
    13
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
asherkin@263
    14
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
asherkin@263
    15
 *
asherkin@263
    16
 * You should have received a copy of the GNU General Public License along with
asherkin@263
    17
 * JITCraft. If not, see http://www.gnu.org/licenses/.
asherkin@263
    18
 */
asherkin@263
    19
#ifndef _include_jitcraft_string_h_
asherkin@263
    20
#define _include_jitcraft_string_h_
asherkin@263
    21
asherkin@263
    22
#include "Heap.h"
asherkin@263
    23
asherkin@263
    24
namespace ke {
asherkin@263
    25
asherkin@263
    26
// A fixed-length string containing UTF-8 characters.
asherkin@263
    27
class String : public Object
asherkin@263
    28
{
asherkin@263
    29
    uint32 length_;
asherkin@263
    30
    
asherkin@263
    31
  public:
asherkin@263
    32
    static String *New(Zone *zone, const char *from, Heap::Tenure tenure = Heap::Tenure_Default);
asherkin@263
    33
    static String *New(Zone *zone, const char *from, size_t length, Heap::Tenure tenure = Heap::Tenure_Default);
asherkin@263
    34
asherkin@263
    35
    unsigned length() {
asherkin@263
    36
        return length_;
asherkin@263
    37
    }
asherkin@263
    38
    char *chars() {
asherkin@263
    39
        return reinterpret_cast<char *>(this + 1);
asherkin@263
    40
    }
asherkin@263
    41
    char *chars(Zone *zone) {
asherkin@263
    42
        return chars();
asherkin@263
    43
    }
asherkin@263
    44
    size_t objSize() {
asherkin@263
    45
        return SizeFor(length());
asherkin@263
    46
    }
asherkin@263
    47
asherkin@263
    48
    static size_t SizeFor(unsigned length) {
asherkin@263
    49
        return sizeof(String) + sizeof(char) * (length + 1);
asherkin@263
    50
    }
asherkin@263
    51
asherkin@263
    52
    static inline String *cast(Object *obj) {
asherkin@263
    53
        assert(!obj || obj->is(MapKind_String));
asherkin@263
    54
        return reinterpret_cast<String *>(obj);
asherkin@263
    55
    }
asherkin@263
    56
};
asherkin@263
    57
asherkin@263
    58
class CharsAndLength
asherkin@263
    59
{
asherkin@263
    60
    const char *str_;
asherkin@263
    61
    size_t length_;
asherkin@263
    62
asherkin@263
    63
  public:
asherkin@263
    64
    CharsAndLength(const char *str, size_t length)
asherkin@263
    65
        : str_(str),
asherkin@263
    66
        length_(length)
asherkin@263
    67
    {
asherkin@263
    68
    }
asherkin@263
    69
asherkin@263
    70
    const char *str() const {
asherkin@263
    71
        return str_;
asherkin@263
    72
    }
asherkin@263
    73
    size_t length() const {
asherkin@263
    74
        return length_;
asherkin@263
    75
    }
asherkin@263
    76
};
asherkin@263
    77
asherkin@263
    78
}
asherkin@263
    79
asherkin@263
    80
#endif // _include_jitcraft_string_h_