src/Strings.cpp
author Asher Baker <asherkin@gmail.com>
Mon Jun 10 01:04:43 2013 +0100 (2013-06-10)
changeset 263 ba85a47ee414
parent 91 5f4402180204
permissions -rw-r--r--
Normalise line endings.
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
#include <string.h>
asherkin@263
    20
#include "Strings.h"
asherkin@263
    21
#include "Zone.h"
asherkin@263
    22
#include "Heap-inl.h"
asherkin@263
    23
asherkin@263
    24
using namespace ke;
asherkin@263
    25
asherkin@263
    26
String *
asherkin@263
    27
String::New(Zone *zone, const char *from, Heap::Tenure tenure)
asherkin@263
    28
{
asherkin@263
    29
    return New(zone, from, strlen(from), tenure);
asherkin@263
    30
}
asherkin@263
    31
asherkin@263
    32
String *
asherkin@263
    33
String::New(Zone *zone, const char *from, size_t length, Heap::Tenure tenure)
asherkin@263
    34
{
asherkin@263
    35
    size_t bytes = SizeFor(length);
asherkin@263
    36
    Local<String> string(zone, String::cast(zone->allocate(MapKind_String, bytes, tenure)));
asherkin@263
    37
    if (!string)
asherkin@263
    38
        return NULL;
asherkin@263
    39
asherkin@263
    40
    string->length_ = length;
asherkin@263
    41
    memcpy(string->chars(), from, length + 1);
asherkin@263
    42
asherkin@263
    43
    return string;
asherkin@263
    44
}