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

Last change on this file since 17372 was 17372, checked in by darin, 19 years ago

Reviewed by Geoff.

  • made changes so the code compiles with the highest warning level under MSVC (disabling some warnings, making some code fixes)
  • API/JSCallbackConstructor.cpp: (KJS::JSCallbackConstructor::construct):
  • API/JSCallbackFunction.cpp: (KJS::JSCallbackFunction::callAsFunction):
  • API/JSCallbackObject.cpp: (KJS::JSCallbackObject::init): (KJS::JSCallbackObject::construct): (KJS::JSCallbackObject::callAsFunction):
  • API/JSObjectRef.cpp: (JSPropertyNameArrayGetNameAtIndex):
  • API/JSStringRef.cpp: (JSStringCreateWithCharacters):
  • JavaScriptCore.vcproj/JavaScriptCore/JavaScriptCore.vcproj:
  • bindings/c/c_utility.cpp: (KJS::Bindings::convertUTF8ToUTF16): (KJS::Bindings::coerceValueToNPVariantStringType): (KJS::Bindings::convertValueToNPVariant):
  • kjs/DateMath.h: (KJS::GregorianDateTime::GregorianDateTime):
  • kjs/ExecState.h: (KJS::ExecState::hadException):
  • kjs/JSImmediate.h: (KJS::JSImmediate::fromDouble): (KJS::JSImmediate::toDouble): (KJS::JSImmediate::NanAsBits): (KJS::JSImmediate::oneAsBits):
  • kjs/Parser.h:
  • kjs/PropertyNameArray.h: (KJS::PropertyNameArray::size):
  • kjs/array_object.cpp: (ArrayObjectImp::callAsFunction):
  • kjs/bool_object.cpp: (BooleanObjectImp::callAsFunction):
  • kjs/collector.cpp: (KJS::Collector::allocate): (KJS::Collector::markCurrentThreadConservatively): (KJS::Collector::collect):
  • kjs/completion.h: (KJS::Completion::isValueCompletion):
  • kjs/date_object.cpp: (KJS::findMonth):
  • kjs/debugger.cpp: (Debugger::sourceParsed): (Debugger::sourceUnused): (Debugger::exception): (Debugger::atStatement): (Debugger::callEvent): (Debugger::returnEvent):
  • kjs/dtoa.cpp:
  • kjs/error_object.cpp: (ErrorObjectImp::callAsFunction): (NativeErrorImp::callAsFunction):
  • kjs/function.cpp: (KJS::FunctionImp::processVarDecls): (KJS::GlobalFuncImp::callAsFunction):
  • kjs/function_object.cpp: (FunctionPrototype::callAsFunction):
  • kjs/grammar.y:
  • kjs/identifier.cpp: (KJS::CStringTranslator::translate): (KJS::Identifier::add):
  • kjs/internal.h:
  • kjs/lexer.cpp: (Lexer::lex): (Lexer::isIdentStart): (Lexer::isIdentPart): (isDecimalDigit): (Lexer::isHexDigit): (Lexer::isOctalDigit): (Lexer::matchPunctuator): (Lexer::singleEscape): (Lexer::convertOctal): (Lexer::convertHex): (Lexer::convertUnicode): (Lexer::record8):
  • kjs/lexer.h:
  • kjs/math_object.cpp: (MathFuncImp::callAsFunction):
  • kjs/number_object.cpp: (integer_part_noexp): (intPow10): (NumberProtoFunc::callAsFunction): (NumberObjectImp::callAsFunction):
  • kjs/object.cpp: (KJS::JSObject::deleteProperty): (KJS::JSObject::callAsFunction): (KJS::JSObject::toBoolean): (KJS::JSObject::toObject):
  • kjs/object.h: (KJS::JSObject::getPropertySlot):
  • kjs/property_map.cpp: (KJS::isValid): (KJS::PropertyMap::put): (KJS::PropertyMap::insert): (KJS::PropertyMap::containsGettersOrSetters):
  • kjs/property_map.h: (KJS::PropertyMap::hasGetterSetterProperties):
  • kjs/property_slot.h:
  • kjs/string_object.cpp: (StringInstance::getPropertyNames): (StringObjectImp::callAsFunction): (StringObjectFuncImp::callAsFunction):
  • kjs/ustring.cpp: (KJS::UString::Rep::computeHash): (KJS::UString::UString): (KJS::UString::from): (KJS::UString::append): (KJS::UString::ascii): (KJS::UString::operator=): (KJS::UString::find): (KJS::UString::rfind):
  • kjs/ustring.h: (KJS::UChar::high): (KJS::UChar::low): (KJS::UCharReference::low): (KJS::UCharReference::high):
  • kjs/value.cpp: (KJS::JSValue::toUInt16):
  • kjs/value.h:
  • pcre/pcre_compile.c: (get_othercase_range):
  • pcre/pcre_exec.c: (match):
  • pcre/pcre_internal.h:
  • wtf/HashFunctions.h: (WTF::intHash): (WTF::PtrHash::hash):
  • wtf/MathExtras.h: (isnan): (lround): (lroundf):
  • wtf/StringExtras.h: (strncasecmp):
  • wtf/unicode/icu/UnicodeIcu.h: (WTF::Unicode::isPrintableChar):
File size: 4.4 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 "APICast.h"
29#include "JSStringRef.h"
30
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: Only works with ASCII
50 // Use decodeUTF8Sequence or http://www.unicode.org/Public/PROGRAMS/CVTUTF/ instead
51 return toRef(UString(string).rep()->ref());
52}
53
54JSStringRef JSStringRetain(JSStringRef string)
55{
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 UString::Rep* aRep = toJS(a);
101 UString::Rep* bRep = toJS(b);
102
103 return UString(aRep) == UString(bRep);
104}
105
106bool JSStringIsEqualToUTF8CString(JSStringRef a, const char* b)
107{
108 JSStringRef bBuf = JSStringCreateWithUTF8CString(b);
109 bool result = JSStringIsEqual(a, bBuf);
110 JSStringRelease(bBuf);
111
112 return result;
113}
114
115#if defined(__APPLE__)
116JSStringRef JSStringCreateWithCFString(CFStringRef string)
117{
118 JSLock lock;
119 CFIndex length = CFStringGetLength(string);
120
121 // Optimized path for when CFString backing store is a UTF16 buffer
122 if (const UniChar* buffer = CFStringGetCharactersPtr(string)) {
123 UString::Rep* rep = UString(reinterpret_cast<const UChar*>(buffer), length).rep()->ref();
124 return toRef(rep);
125 }
126
127 UniChar* buffer = static_cast<UniChar*>(fastMalloc(sizeof(UniChar) * length));
128 CFStringGetCharacters(string, CFRangeMake(0, length), buffer);
129 UString::Rep* rep = UString(reinterpret_cast<UChar*>(buffer), length, false).rep()->ref();
130 return toRef(rep);
131}
132
133CFStringRef JSStringCopyCFString(CFAllocatorRef alloc, JSStringRef string)
134{
135 UString::Rep* rep = toJS(string);
136 return CFStringCreateWithCharacters(alloc, reinterpret_cast<const JSChar*>(rep->data()), rep->size());
137}
138
139#endif // __APPLE__
Note: See TracBrowser for help on using the repository browser.