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

Last change on this file since 15400 was 15400, checked in by ggaren, 19 years ago

RS by Beth.


Finished previously approved JSInternalString -> JSString conversion
by renaming the files.

  • API/JSCallbackObject.cpp:
  • API/JSInternalStringRef.cpp: Removed.
  • API/JSInternalStringRef.h: Removed.
  • API/JSStringRef.cpp: Added.
  • API/JSStringRef.h: Added.
  • API/JavaScriptCore.h:
  • JavaScriptCore.xcodeproj/project.pbxproj:
File size: 4.9 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 "APICast.h"
28#include "JSStringRef.h"
29
30#include <kjs/JSLock.h>
31#include <kjs/JSType.h>
32#include <kjs/internal.h>
33#include <kjs/operations.h>
34#include <kjs/ustring.h>
35#include <kjs/value.h>
36
37using namespace KJS;
38
39JSValueRef JSValueMakeString(JSStringRef string)
40{
41 JSLock lock;
42 UString::Rep* rep = toJS(string);
43 return toRef(jsString(UString(rep)));
44}
45
46JSStringRef JSStringCreateWithCharacters(const JSChar* chars, size_t numChars)
47{
48 JSLock lock;
49 return toRef(UString(reinterpret_cast<const UChar*>(chars), numChars).rep()->ref());
50}
51
52JSStringRef JSStringCreateWithUTF8CString(const char* string)
53{
54 JSLock lock;
55 // FIXME: Only works with ASCII
56 // Use decodeUTF8Sequence or http://www.unicode.org/Public/PROGRAMS/CVTUTF/ instead
57 return toRef(UString(string).rep()->ref());
58}
59
60JSStringRef JSStringRetain(JSStringRef string)
61{
62 UString::Rep* rep = toJS(string);
63 return toRef(rep->ref());
64}
65
66void JSStringRelease(JSStringRef string)
67{
68 JSLock lock;
69 UString::Rep* rep = toJS(string);
70 rep->deref();
71}
72
73JSStringRef JSValueToStringCopy(JSContextRef context, JSValueRef value)
74{
75 JSLock lock;
76 JSValue* jsValue = toJS(value);
77 ExecState* exec = toJS(context);
78
79 JSStringRef stringRef = toRef(jsValue->toString(exec).rep()->ref());
80 if (exec->hadException())
81 exec->clearException();
82 return stringRef;
83}
84
85size_t JSStringGetLength(JSStringRef string)
86{
87 UString::Rep* rep = toJS(string);
88 return rep->size();
89}
90
91const JSChar* JSStringGetCharactersPtr(JSStringRef string)
92{
93 UString::Rep* rep = toJS(string);
94 return reinterpret_cast<const JSChar*>(rep->data());
95}
96
97size_t JSStringGetMaximumUTF8CStringSize(JSStringRef string)
98{
99 UString::Rep* rep = toJS(string);
100
101 // Any UTF8 character > 3 bytes encodes as a UTF16 surrogate pair.
102 return rep->size() * 3 + 1; // + 1 for terminating '\0'
103}
104
105size_t JSStringGetUTF8CString(JSStringRef string, char* buffer, size_t bufferSize)
106{
107 JSLock lock;
108 UString::Rep* rep = toJS(string);
109 CString cString = UString(rep).UTF8String();
110
111 size_t length = std::min(bufferSize, cString.size() + 1); // + 1 for terminating '\0'
112 memcpy(buffer, cString.c_str(), length);
113 return length;
114}
115
116bool JSStringIsEqual(JSStringRef a, JSStringRef b)
117{
118 UString::Rep* aRep = toJS(a);
119 UString::Rep* bRep = toJS(b);
120
121 return UString(aRep) == UString(bRep);
122}
123
124bool JSStringIsEqualToUTF8CString(JSStringRef a, const char* b)
125{
126 JSStringRef bBuf = JSStringCreateWithUTF8CString(b);
127 bool result = JSStringIsEqual(a, bBuf);
128 JSStringRelease(bBuf);
129
130 return result;
131}
132
133#if defined(__APPLE__)
134JSStringRef JSStringCreateWithCFString(CFStringRef string)
135{
136 JSLock lock;
137 CFIndex length = CFStringGetLength(string);
138
139 // Optimized path for when CFString backing store is a UTF16 buffer
140 if (const UniChar* buffer = CFStringGetCharactersPtr(string)) {
141 UString::Rep* rep = UString(reinterpret_cast<const UChar*>(buffer), length).rep()->ref();
142 return toRef(rep);
143 }
144
145 UniChar* buffer = static_cast<UniChar*>(fastMalloc(sizeof(UniChar) * length));
146 CFStringGetCharacters(string, CFRangeMake(0, length), buffer);
147 UString::Rep* rep = UString(reinterpret_cast<UChar*>(buffer), length, false).rep()->ref();
148 return toRef(rep);
149}
150
151CFStringRef JSStringCopyCFString(CFAllocatorRef alloc, JSStringRef string)
152{
153 UString::Rep* rep = toJS(string);
154 return CFStringCreateWithCharacters(alloc, reinterpret_cast<const JSChar*>(rep->data()), rep->size());
155}
156
157#endif // __APPLE__
Note: See TracBrowser for help on using the repository browser.