jitcraft/Strings.cpp
changeset 54 8e22792a3921
child 59 b7d61ddb3c8d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/jitcraft/Strings.cpp	Sat Jul 07 20:28:29 2012 -0700
     1.3 @@ -0,0 +1,43 @@
     1.4 +/* vim: set ts=4 sw=4 tw=99 et:
     1.5 + *
     1.6 + * Copyright (C) 2012 David Anderson
     1.7 + *
     1.8 + * This file is part of JITCraft.
     1.9 + *
    1.10 + * JITCraft is free software: you can redistribute it and/or modify it under
    1.11 + * the terms of the GNU General Public License as published by the Free
    1.12 + * Software Foundation, either version 3 of the License, or (at your option)
    1.13 + * any later version.
    1.14 + * 
    1.15 + * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    1.16 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    1.18 + *
    1.19 + * You should have received a copy of the GNU General Public License along with
    1.20 + * JITCraft. If not, see http://www.gnu.org/licenses/.
    1.21 + */
    1.22 +#include "Strings.h"
    1.23 +#include "Zone.h"
    1.24 +#include "Heap-inl.h"
    1.25 +
    1.26 +using namespace jc;
    1.27 +
    1.28 +String *
    1.29 +String::New(Zone *zone, const char *from, Heap::Tenure tenure)
    1.30 +{
    1.31 +    return New(zone, from, strlen(from), tenure);
    1.32 +}
    1.33 +
    1.34 +String *
    1.35 +String::New(Zone *zone, const char *from, size_t length, Heap::Tenure tenure)
    1.36 +{
    1.37 +    size_t bytes = SizeFor(length);
    1.38 +    Local<String> string(zone, String::cast(zone->allocate(MapKind_String, bytes, tenure)));
    1.39 +    if (!string)
    1.40 +        return NULL;
    1.41 +
    1.42 +    string->length_ = length;
    1.43 +    memcpy(string->chars(), from, length + 1);
    1.44 +
    1.45 +    return string;
    1.46 +}