source: webkit/trunk/JavaScriptCore/API/JSStringRef.cpp@ 20362

Last change on this file since 20362 was 20362, checked in by adele, 18 years ago

Reviewed by Geoff.

  • API/JSStringRef.cpp: (JSStringIsEqual): Added JSLock.
File size: 3.5 KB
Line 
1// -*- mode: c++; c-basic-offset: 4 -*-
2/*
3 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <wtf/Platform.h>
28#include "JSStringRef.h"
29
30#include "APICast.h"
31#include <kjs/JSLock.h>
32#include <kjs/JSType.h>
33#include <kjs/internal.h>
34#include <kjs/operations.h>
35#include <kjs/ustring.h>
36#include <kjs/value.h>
37
38using namespace KJS;
39
40JSStringRef JSStringCreateWithCharacters(const JSChar* chars, size_t numChars)
41{
42 JSLock lock;
43 return toRef(UString(reinterpret_cast<const UChar*>(chars), static_cast<int>(numChars)).rep()->ref());
44}
45
46JSStringRef JSStringCreateWithUTF8CString(const char* string)
47{
48 JSLock lock;
49 // FIXME: <rdar://problem/4949018>
50 return toRef(UString(string).rep()->ref());
51}
52
53JSStringRef JSStringRetain(JSStringRef string)
54{
55 JSLock lock;
56 UString::Rep* rep = toJS(string);
57 return toRef(rep->ref());
58}
59
60void JSStringRelease(JSStringRef string)
61{
62 JSLock lock;
63 UString::Rep* rep = toJS(string);
64 rep->deref();
65}
66
67size_t JSStringGetLength(JSStringRef string)
68{
69 UString::Rep* rep = toJS(string);
70 return rep->size();
71}
72
73const JSChar* JSStringGetCharactersPtr(JSStringRef string)
74{
75 UString::Rep* rep = toJS(string);
76 return reinterpret_cast<const JSChar*>(rep->data());
77}
78
79size_t JSStringGetMaximumUTF8CStringSize(JSStringRef string)
80{
81 UString::Rep* rep = toJS(string);
82
83 // Any UTF8 character > 3 bytes encodes as a UTF16 surrogate pair.
84 return rep->size() * 3 + 1; // + 1 for terminating '\0'
85}
86
87size_t JSStringGetUTF8CString(JSStringRef string, char* buffer, size_t bufferSize)
88{
89 JSLock lock;
90 UString::Rep* rep = toJS(string);
91 CString cString = UString(rep).UTF8String();
92
93 size_t length = std::min(bufferSize, cString.size() + 1); // + 1 for terminating '\0'
94 memcpy(buffer, cString.c_str(), length);
95 return length;
96}
97
98bool JSStringIsEqual(JSStringRef a, JSStringRef b)
99{
100 JSLock lock;
101
102 UString::Rep* aRep = toJS(a);
103 UString::Rep* bRep = toJS(b);
104
105 return UString(aRep) == UString(bRep);
106}
107
108bool JSStringIsEqualToUTF8CString(JSStringRef a, const char* b)
109{
110 JSStringRef bBuf = JSStringCreateWithUTF8CString(b);
111 bool result = JSStringIsEqual(a, bBuf);
112 JSStringRelease(bBuf);
113
114 return result;
115}
Note: See TracBrowser for help on using the repository browser.