diff options
author | maruel@google.com <maruel@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-10-08 20:53:18 +0000 |
---|---|---|
committer | maruel@google.com <maruel@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-10-08 20:53:18 +0000 |
commit | a8964f6d5af2d472f7a1339ab85cebc12501280d (patch) | |
tree | 4d2f81e40a1e0114e2b634fd9cb54a9b33abb3dc /webkit/pending | |
parent | e617a31ce690976e4d73316b4c0c3d0550745475 (diff) | |
download | chromium_src-a8964f6d5af2d472f7a1339ab85cebc12501280d.zip chromium_src-a8964f6d5af2d472f7a1339ab85cebc12501280d.tar.gz chromium_src-a8964f6d5af2d472f7a1339ab85cebc12501280d.tar.bz2 |
Mac build breakage. Revert 3035 & 3040.
Review URL: http://codereview.chromium.org/6357
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@3045 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/pending')
-rw-r--r-- | webkit/pending/DeleteButtonController.cpp | 308 | ||||
-rw-r--r-- | webkit/pending/HTMLAnchorElement.cpp | 504 | ||||
-rw-r--r-- | webkit/pending/HTMLLinkElement.cpp | 403 | ||||
-rw-r--r-- | webkit/pending/HTMLLinkElement.h | 119 | ||||
-rw-r--r-- | webkit/pending/String.cpp | 845 | ||||
-rw-r--r-- | webkit/pending/StringImpl.cpp | 1051 | ||||
-rw-r--r-- | webkit/pending/StringImpl.h | 282 |
7 files changed, 3512 insertions, 0 deletions
diff --git a/webkit/pending/DeleteButtonController.cpp b/webkit/pending/DeleteButtonController.cpp new file mode 100644 index 0000000..d00220d --- /dev/null +++ b/webkit/pending/DeleteButtonController.cpp @@ -0,0 +1,308 @@ +/* + * Copyright (C) 2006, 2008 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "DeleteButtonController.h" + +#include "CachedImage.h" +#include "CSSMutableStyleDeclaration.h" +#include "CSSPrimitiveValue.h" +#include "CSSPropertyNames.h" +#include "CSSValueKeywords.h" +#include "DeleteButton.h" +#include "Document.h" +#include "Editor.h" +#include "Frame.h" +#include "htmlediting.h" +#include "HTMLDivElement.h" +#include "HTMLNames.h" +#include "Image.h" +#include "Node.h" +#include "Range.h" +#include "RemoveNodeCommand.h" +#include "RenderObject.h" +#include "SelectionController.h" + +namespace WebCore { + +using namespace HTMLNames; + +const char* const DeleteButtonController::containerElementIdentifier = "WebKit-Editing-Delete-Container"; +const char* const DeleteButtonController::buttonElementIdentifier = "WebKit-Editing-Delete-Button"; +const char* const DeleteButtonController::outlineElementIdentifier = "WebKit-Editing-Delete-Outline"; + +DeleteButtonController::DeleteButtonController(Frame* frame) + : m_frame(frame) + , m_wasStaticPositioned(false) + , m_wasAutoZIndex(false) + , m_disableStack(0) +{ +} + +static bool isDeletableElement(const Node* node) +{ + if (!node || !node->isHTMLElement() || !node->inDocument() || !node->isContentEditable()) + return false; + + const int minimumWidth = 25; + const int minimumHeight = 25; + const unsigned minimumVisibleBorders = 3; + + RenderObject* renderer = node->renderer(); + if (!renderer || renderer->width() < minimumWidth || renderer->height() < minimumHeight) + return false; + + if (renderer->isTable()) + return true; + + if (node->hasTagName(ulTag) || node->hasTagName(olTag)) + return true; + + if (renderer->isPositioned()) + return true; + + // allow block elements (excluding table cells) that have some non-transparent borders + if (renderer->isRenderBlock() && !renderer->isTableCell()) { + RenderStyle* style = renderer->style(); + if (style && style->hasBorder()) { + unsigned visibleBorders = style->borderTop().isVisible() + style->borderBottom().isVisible() + style->borderLeft().isVisible() + style->borderRight().isVisible(); + if (visibleBorders >= minimumVisibleBorders) + return true; + } + } + + return false; +} + +static HTMLElement* enclosingDeletableElement(const Selection& selection) +{ + if (!selection.isContentEditable()) + return 0; + + RefPtr<Range> range = selection.toRange(); + if (!range) + return 0; + + ExceptionCode ec = 0; + Node* container = range->commonAncestorContainer(ec); + ASSERT(container); + ASSERT(ec == 0); + + // The enclosingNodeOfType function only works on nodes that are editable + // (which is strange, given its name). + if (!container->isContentEditable()) + return 0; + + Node* element = enclosingNodeOfType(Position(container, 0), &isDeletableElement); + if (!element) + return 0; + + ASSERT(element->isHTMLElement()); + return static_cast<HTMLElement*>(element); +} + +void DeleteButtonController::respondToChangedSelection(const Selection& oldSelection) +{ + if (!enabled()) + return; + + HTMLElement* oldElement = enclosingDeletableElement(oldSelection); + HTMLElement* newElement = enclosingDeletableElement(m_frame->selection()->selection()); + if (oldElement == newElement) + return; + + // If the base is inside a deletable element, give the element a delete widget. + if (newElement) + show(newElement); + else + hide(); +} + +void DeleteButtonController::createDeletionUI() +{ + RefPtr<HTMLDivElement> container = new HTMLDivElement(m_target->document()); + container->setId(containerElementIdentifier); + + CSSMutableStyleDeclaration* style = container->getInlineStyleDecl(); + style->setProperty(CSSPropertyWebkitUserDrag, CSSValueNone); + style->setProperty(CSSPropertyWebkitUserSelect, CSSValueNone); + style->setProperty(CSSPropertyWebkitUserModify, CSSValueNone); + + RefPtr<HTMLDivElement> outline = new HTMLDivElement(m_target->document()); + outline->setId(outlineElementIdentifier); + + const int borderWidth = 4; + const int borderRadius = 6; + + style = outline->getInlineStyleDecl(); + style->setProperty(CSSPropertyPosition, CSSValueAbsolute); + style->setProperty(CSSPropertyCursor, CSSValueDefault); + style->setProperty(CSSPropertyWebkitUserDrag, CSSValueNone); + style->setProperty(CSSPropertyWebkitUserSelect, CSSValueNone); + style->setProperty(CSSPropertyWebkitUserModify, CSSValueNone); + style->setProperty(CSSPropertyZIndex, String::number(-1000000)); + style->setProperty(CSSPropertyTop, String::number(-borderWidth - m_target->renderer()->borderTop()) + "px"); + style->setProperty(CSSPropertyRight, String::number(-borderWidth - m_target->renderer()->borderRight()) + "px"); + style->setProperty(CSSPropertyBottom, String::number(-borderWidth - m_target->renderer()->borderBottom()) + "px"); + style->setProperty(CSSPropertyLeft, String::number(-borderWidth - m_target->renderer()->borderLeft()) + "px"); + style->setProperty(CSSPropertyBorder, String::number(borderWidth) + "px solid rgba(0, 0, 0, 0.6)"); + style->setProperty(CSSPropertyWebkitBorderRadius, String::number(borderRadius) + "px"); + + ExceptionCode ec = 0; + container->appendChild(outline.get(), ec); + ASSERT(ec == 0); + if (ec) + return; + + RefPtr<DeleteButton> button = new DeleteButton(m_target->document()); + button->setId(buttonElementIdentifier); + + const int buttonWidth = 30; + const int buttonHeight = 30; + const int buttonBottomShadowOffset = 2; + + style = button->getInlineStyleDecl(); + style->setProperty(CSSPropertyPosition, CSSValueAbsolute); + style->setProperty(CSSPropertyCursor, CSSValueDefault); + style->setProperty(CSSPropertyWebkitUserDrag, CSSValueNone); + style->setProperty(CSSPropertyWebkitUserSelect, CSSValueNone); + style->setProperty(CSSPropertyWebkitUserModify, CSSValueNone); + style->setProperty(CSSPropertyZIndex, String::number(1000000)); + style->setProperty(CSSPropertyTop, String::number((-buttonHeight / 2) - m_target->renderer()->borderTop() - (borderWidth / 2) + buttonBottomShadowOffset) + "px"); + style->setProperty(CSSPropertyLeft, String::number((-buttonWidth / 2) - m_target->renderer()->borderLeft() - (borderWidth / 2)) + "px"); + style->setProperty(CSSPropertyWidth, String::number(buttonWidth) + "px"); + style->setProperty(CSSPropertyHeight, String::number(buttonHeight) + "px"); + + RefPtr<Image> buttonImage = Image::loadPlatformResource("deleteButton"); + if (buttonImage->isNull()) + return; + + button->setCachedImage(new CachedImage(buttonImage.get())); + + container->appendChild(button.get(), ec); + ASSERT(ec == 0); + if (ec) + return; + + m_containerElement = container.release(); + m_outlineElement = outline.release(); + m_buttonElement = button.release(); +} + +void DeleteButtonController::show(HTMLElement* element) +{ + hide(); + + if (!enabled() || !element || !element->inDocument() || !isDeletableElement(element)) + return; + + if (!m_frame->editor()->shouldShowDeleteInterface(static_cast<HTMLElement*>(element))) + return; + + // we rely on the renderer having current information, so we should update the layout if needed + m_frame->document()->updateLayoutIgnorePendingStylesheets(); + + m_target = element; + + if (!m_containerElement) { + createDeletionUI(); + if (!m_containerElement) { + hide(); + return; + } + } + + ExceptionCode ec = 0; + m_target->appendChild(m_containerElement.get(), ec); + ASSERT(ec == 0); + if (ec) { + hide(); + return; + } + + if (m_target->renderer()->style()->position() == StaticPosition) { + m_target->getInlineStyleDecl()->setProperty(CSSPropertyPosition, CSSValueRelative); + m_wasStaticPositioned = true; + } + + if (m_target->renderer()->style()->hasAutoZIndex()) { + m_target->getInlineStyleDecl()->setProperty(CSSPropertyZIndex, "0"); + m_wasAutoZIndex = true; + } +} + +void DeleteButtonController::hide() +{ + m_outlineElement = 0; + m_buttonElement = 0; + + ExceptionCode ec = 0; + if (m_containerElement && m_containerElement->parentNode()) + m_containerElement->parentNode()->removeChild(m_containerElement.get(), ec); + + if (m_target) { + if (m_wasStaticPositioned) + m_target->getInlineStyleDecl()->setProperty(CSSPropertyPosition, CSSValueStatic); + if (m_wasAutoZIndex) + m_target->getInlineStyleDecl()->setProperty(CSSPropertyZIndex, CSSValueAuto); + } + + m_wasStaticPositioned = false; + m_wasAutoZIndex = false; +} + +void DeleteButtonController::enable() +{ + ASSERT(m_disableStack > 0); + if (m_disableStack > 0) + m_disableStack--; + if (enabled()) + show(enclosingDeletableElement(m_frame->selection()->selection())); +} + +void DeleteButtonController::disable() +{ + if (enabled()) + hide(); + m_disableStack++; +} + +void DeleteButtonController::deleteTarget() +{ + if (!enabled() || !m_target) + return; + + RefPtr<Node> element = m_target; + hide(); + + // Because the deletion UI only appears when the selection is entirely + // within the target, we unconditionally update the selection to be + // a caret where the target had been. + Position pos = positionBeforeNode(element.get()); + applyCommand(RemoveNodeCommand::create(element.release())); + m_frame->selection()->setSelection(VisiblePosition(pos)); +} + +} // namespace WebCore diff --git a/webkit/pending/HTMLAnchorElement.cpp b/webkit/pending/HTMLAnchorElement.cpp new file mode 100644 index 0000000..3289189 --- /dev/null +++ b/webkit/pending/HTMLAnchorElement.cpp @@ -0,0 +1,504 @@ +/* + * Copyright (C) 1999 Lars Knoll (knoll@kde.org) + * (C) 1999 Antti Koivisto (koivisto@kde.org) + * (C) 2000 Simon Hausmann <hausmann@kde.org> + * Copyright (C) 2003, 2006, 2007, 2008 Apple Inc. All rights reserved. + * (C) 2006 Graham Dennis (graham.dennis@gmail.com) + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "config.h" +#include "HTMLAnchorElement.h" + +#include "CSSHelper.h" +#include "DNS.h" +#include "Document.h" +#include "Event.h" +#include "EventHandler.h" +#include "EventNames.h" +#include "Frame.h" +#include "FrameLoader.h" +#include "FrameLoaderClient.h" +#include "HTMLImageElement.h" +#include "HTMLNames.h" +#include "KeyboardEvent.h" +#include "MouseEvent.h" +#include "MutationEvent.h" +#include "RenderFlow.h" +#include "RenderImage.h" +#include "ResourceRequest.h" +#include "SelectionController.h" +#include "Settings.h" +#include "UIEvent.h" + +namespace WebCore { + +using namespace HTMLNames; +using namespace EventNames; + +HTMLAnchorElement::HTMLAnchorElement(Document* doc) + : HTMLElement(aTag, doc) + , m_rootEditableElementForSelectionOnMouseDown(0) + , m_wasShiftKeyDownOnMouseDown(false) +{ +} + +HTMLAnchorElement::HTMLAnchorElement(const QualifiedName& tagName, Document* doc) + : HTMLElement(tagName, doc) + , m_rootEditableElementForSelectionOnMouseDown(0) + , m_wasShiftKeyDownOnMouseDown(false) +{ +} + +HTMLAnchorElement::~HTMLAnchorElement() +{ +} + +bool HTMLAnchorElement::supportsFocus() const +{ + if (isContentEditable()) + return HTMLElement::supportsFocus(); + return isFocusable() || (isLink() && document() && !document()->haveStylesheetsLoaded()); +} + +bool HTMLAnchorElement::isFocusable() const +{ + if (isContentEditable()) + return HTMLElement::isFocusable(); + + // FIXME: Even if we are not visible, we might have a child that is visible. + // Dave wants to fix that some day with a "has visible content" flag or the like. + if (!(isLink() && renderer() && renderer()->style()->visibility() == VISIBLE)) + return false; + + return true; +} + +bool HTMLAnchorElement::isMouseFocusable() const +{ + return false; +} + +bool HTMLAnchorElement::isKeyboardFocusable(KeyboardEvent* event) const +{ + if (!isFocusable()) + return false; + + if (!document()->frame()) + return false; + + if (!document()->frame()->eventHandler()->tabsToLinks(event)) + return false; + + // Before calling absoluteRects, check for the common case where the renderer + // or one of the continuations is non-empty, since this is a faster check and + // almost always returns true. + for (RenderObject* r = renderer(); r; r = r->continuation()) + if (r->width() > 0 && r->height() > 0) + return true; + + Vector<IntRect> rects; + int x, y; + renderer()->absolutePosition(x, y); + renderer()->absoluteRects(rects, x, y); + size_t n = rects.size(); + for (size_t i = 0; i < n; ++i) + if (!rects[i].isEmpty()) + return true; + + return false; +} + +void HTMLAnchorElement::defaultEventHandler(Event* evt) +{ + // React on clicks and on keypresses. + // Don't make this KEYUP_EVENT again, it makes khtml follow links it shouldn't, + // when pressing Enter in the combo. + if (isLink() && (evt->type() == clickEvent || (evt->type() == keydownEvent && focused()))) { + MouseEvent* e = 0; + if (evt->type() == clickEvent && evt->isMouseEvent()) + e = static_cast<MouseEvent*>(evt); + + KeyboardEvent* k = 0; + if (evt->type() == keydownEvent && evt->isKeyboardEvent()) + k = static_cast<KeyboardEvent*>(evt); + + if (e && e->button() == RightButton) { + HTMLElement::defaultEventHandler(evt); + return; + } + + // If the link is editable, then we need to check the settings to see whether or not to follow the link + if (isContentEditable()) { + EditableLinkBehavior editableLinkBehavior = EditableLinkDefaultBehavior; + if (Settings* settings = document()->settings()) + editableLinkBehavior = settings->editableLinkBehavior(); + + switch (editableLinkBehavior) { + // Always follow the link (Safari 2.0 behavior) + default: + case EditableLinkDefaultBehavior: + case EditableLinkAlwaysLive: + break; + + case EditableLinkNeverLive: + HTMLElement::defaultEventHandler(evt); + return; + + // If the selection prior to clicking on this link resided in the same editable block as this link, + // and the shift key isn't pressed, we don't want to follow the link + case EditableLinkLiveWhenNotFocused: + if (e && !e->shiftKey() && m_rootEditableElementForSelectionOnMouseDown == rootEditableElement()) { + HTMLElement::defaultEventHandler(evt); + return; + } + break; + + // Only follow the link if the shift key is down (WinIE/Firefox behavior) + case EditableLinkOnlyLiveWithShiftKey: + if (e && !e->shiftKey()) { + HTMLElement::defaultEventHandler(evt); + return; + } + break; + } + } + + if (k) { + if (k->keyIdentifier() != "Enter") { + HTMLElement::defaultEventHandler(evt); + return; + } + evt->setDefaultHandled(); + dispatchSimulatedClick(evt); + return; + } + + String url = parseURL(getAttribute(hrefAttr)); + + ASSERT(evt->target()); + ASSERT(evt->target()->toNode()); + if (evt->target()->toNode()->hasTagName(imgTag)) { + HTMLImageElement* img = static_cast<HTMLImageElement*>(evt->target()->toNode()); + if (img && img->isServerMap()) { + RenderImage* r = static_cast<RenderImage*>(img->renderer()); + if (r && e) { + int absx, absy; + r->absolutePosition(absx, absy); + int x = e->pageX() - absx; + int y = e->pageY() - absy; + url += "?"; + url += String::number(x); + url += ","; + url += String::number(y); + } else { + evt->setDefaultHandled(); + HTMLElement::defaultEventHandler(evt); + return; + } + } + } + + if (!evt->defaultPrevented() && document()->frame()) + document()->frame()->loader()->urlSelected(document()->completeURL(url), getAttribute(targetAttr), evt, false, true); + + evt->setDefaultHandled(); + } else if (isLink() && isContentEditable()) { + // This keeps track of the editable block that the selection was in (if it was in one) just before the link was clicked + // for the LiveWhenNotFocused editable link behavior + if (evt->type() == mousedownEvent && evt->isMouseEvent() && static_cast<MouseEvent*>(evt)->button() != RightButton && document()->frame() && document()->frame()->selection()) { + MouseEvent* e = static_cast<MouseEvent*>(evt); + + m_rootEditableElementForSelectionOnMouseDown = document()->frame()->selection()->rootEditableElement(); + m_wasShiftKeyDownOnMouseDown = e && e->shiftKey(); + } else if (evt->type() == mouseoverEvent) { + // These are cleared on mouseover and not mouseout because their values are needed for drag events, but these happen + // after mouse out events. + m_rootEditableElementForSelectionOnMouseDown = 0; + m_wasShiftKeyDownOnMouseDown = false; + } + } + + HTMLElement::defaultEventHandler(evt); +} + +void HTMLAnchorElement::setActive(bool down, bool pause) +{ + if (isContentEditable()) { + EditableLinkBehavior editableLinkBehavior = EditableLinkDefaultBehavior; + if (Settings* settings = document()->settings()) + editableLinkBehavior = settings->editableLinkBehavior(); + + switch(editableLinkBehavior) { + default: + case EditableLinkDefaultBehavior: + case EditableLinkAlwaysLive: + break; + + case EditableLinkNeverLive: + return; + + // Don't set the link to be active if the current selection is in the same editable block as + // this link + case EditableLinkLiveWhenNotFocused: + if (down && document()->frame() && document()->frame()->selection() && + document()->frame()->selection()->rootEditableElement() == rootEditableElement()) + return; + break; + + case EditableLinkOnlyLiveWithShiftKey: + return; + } + + } + + ContainerNode::setActive(down, pause); +} + +void HTMLAnchorElement::parseMappedAttribute(MappedAttribute *attr) +{ + if (attr->name() == hrefAttr) { + bool wasLink = isLink(); + setIsLink(!attr->isNull()); + if (wasLink != isLink()) + setChanged(); + if (isLink() && document()->isDNSPrefetchEnabled()) { + String value = attr->value(); + if (protocolIs(value, "http") || protocolIs(value, "https") || value.startsWith("//")) + prefetchDNS(document()->completeURL(value).host()); + } + } else if (attr->name() == nameAttr || + attr->name() == titleAttr || + attr->name() == relAttr) { + // Do nothing. + } else + HTMLElement::parseMappedAttribute(attr); +} + +void HTMLAnchorElement::accessKeyAction(bool sendToAnyElement) +{ + // send the mouse button events if the caller specified sendToAnyElement + dispatchSimulatedClick(0, sendToAnyElement); +} + +bool HTMLAnchorElement::isURLAttribute(Attribute *attr) const +{ + return attr->name() == hrefAttr; +} + +bool HTMLAnchorElement::canStartSelection() const +{ + // FIXME: We probably want this same behavior in SVGAElement too + if (!isLink()) + return HTMLElement::canStartSelection(); + return isContentEditable(); +} + +String HTMLAnchorElement::accessKey() const +{ + return getAttribute(accesskeyAttr); +} + +void HTMLAnchorElement::setAccessKey(const String &value) +{ + setAttribute(accesskeyAttr, value); +} + +String HTMLAnchorElement::charset() const +{ + return getAttribute(charsetAttr); +} + +void HTMLAnchorElement::setCharset(const String &value) +{ + setAttribute(charsetAttr, value); +} + +String HTMLAnchorElement::coords() const +{ + return getAttribute(coordsAttr); +} + +void HTMLAnchorElement::setCoords(const String &value) +{ + setAttribute(coordsAttr, value); +} + +KURL HTMLAnchorElement::href() const +{ + return document()->completeURL(getAttribute(hrefAttr)); +} + +void HTMLAnchorElement::setHref(const String &value) +{ + setAttribute(hrefAttr, value); +} + +String HTMLAnchorElement::hreflang() const +{ + return getAttribute(hreflangAttr); +} + +void HTMLAnchorElement::setHreflang(const String &value) +{ + setAttribute(hreflangAttr, value); +} + +String HTMLAnchorElement::name() const +{ + return getAttribute(nameAttr); +} + +void HTMLAnchorElement::setName(const String &value) +{ + setAttribute(nameAttr, value); +} + +String HTMLAnchorElement::rel() const +{ + return getAttribute(relAttr); +} + +void HTMLAnchorElement::setRel(const String &value) +{ + setAttribute(relAttr, value); +} + +String HTMLAnchorElement::rev() const +{ + return getAttribute(revAttr); +} + +void HTMLAnchorElement::setRev(const String &value) +{ + setAttribute(revAttr, value); +} + +String HTMLAnchorElement::shape() const +{ + return getAttribute(shapeAttr); +} + +void HTMLAnchorElement::setShape(const String &value) +{ + setAttribute(shapeAttr, value); +} + +short HTMLAnchorElement::tabIndex() const +{ + return Element::tabIndex(); +} + +String HTMLAnchorElement::target() const +{ + return getAttribute(targetAttr); +} + +void HTMLAnchorElement::setTarget(const String &value) +{ + setAttribute(targetAttr, value); +} + +String HTMLAnchorElement::type() const +{ + return getAttribute(typeAttr); +} + +void HTMLAnchorElement::setType(const String &value) +{ + setAttribute(typeAttr, value); +} + +String HTMLAnchorElement::hash() const +{ + return "#" + href().ref(); +} + +String HTMLAnchorElement::host() const +{ + return href().host(); +} + +String HTMLAnchorElement::hostname() const +{ + const KURL& url = href(); + if (url.port() == 0) + return url.host(); + return url.host() + ":" + String::number(url.port()); +} + +String HTMLAnchorElement::pathname() const +{ + return href().path(); +} + +String HTMLAnchorElement::port() const +{ + return String::number(href().port()); +} + +String HTMLAnchorElement::protocol() const +{ + return href().protocol() + ":"; +} + +String HTMLAnchorElement::search() const +{ + return href().query(); +} + +String HTMLAnchorElement::text() const +{ + return innerText(); +} + +String HTMLAnchorElement::toString() const +{ + return href().string(); +} + +bool HTMLAnchorElement::isLiveLink() const +{ + if (!isLink()) + return false; + if (!isContentEditable()) + return true; + + EditableLinkBehavior editableLinkBehavior = EditableLinkDefaultBehavior; + if (Settings* settings = document()->settings()) + editableLinkBehavior = settings->editableLinkBehavior(); + + switch(editableLinkBehavior) { + default: + case EditableLinkDefaultBehavior: + case EditableLinkAlwaysLive: + return true; + + case EditableLinkNeverLive: + return false; + + // Don't set the link to be live if the current selection is in the same editable block as + // this link or if the shift key is down + case EditableLinkLiveWhenNotFocused: + return m_wasShiftKeyDownOnMouseDown || m_rootEditableElementForSelectionOnMouseDown != rootEditableElement(); + + case EditableLinkOnlyLiveWithShiftKey: + return m_wasShiftKeyDownOnMouseDown; + } +} + +} diff --git a/webkit/pending/HTMLLinkElement.cpp b/webkit/pending/HTMLLinkElement.cpp new file mode 100644 index 0000000..893f15d --- /dev/null +++ b/webkit/pending/HTMLLinkElement.cpp @@ -0,0 +1,403 @@ +/* + * Copyright (C) 1999 Lars Knoll (knoll@kde.org) + * (C) 1999 Antti Koivisto (koivisto@kde.org) + * (C) 2001 Dirk Mueller (mueller@kde.org) + * Copyright (C) 2003, 2008 Apple Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "config.h" +#include "HTMLLinkElement.h" + +#include "CSSHelper.h" +#include "CachedCSSStyleSheet.h" +#include "DNS.h" +#include "DocLoader.h" +#include "Document.h" +#include "Frame.h" +#include "FrameLoader.h" +#include "FrameLoaderClient.h" +#include "FrameTree.h" +#include "HTMLNames.h" +#include "MediaList.h" +#include "MediaQueryEvaluator.h" +#include "Page.h" +#include "Settings.h" + +namespace WebCore { + +using namespace HTMLNames; + +HTMLLinkElement::HTMLLinkElement(Document *doc) + : HTMLElement(linkTag, doc) + , m_cachedSheet(0) + , m_disabledState(0) + , m_loading(false) + , m_alternate(false) + , m_isStyleSheet(false) + , m_isIcon(false) + , m_isDNSPrefetch(false) + , m_createdByParser(false) +{ +} + +HTMLLinkElement::~HTMLLinkElement() +{ + if (m_cachedSheet) { + m_cachedSheet->removeClient(this); + if (m_loading && !isDisabled() && !isAlternate()) + document()->removePendingSheet(); + } +} + +void HTMLLinkElement::setDisabledState(bool _disabled) +{ + int oldDisabledState = m_disabledState; + m_disabledState = _disabled ? 2 : 1; + if (oldDisabledState != m_disabledState) { + // If we change the disabled state while the sheet is still loading, then we have to + // perform three checks: + if (isLoading()) { + // Check #1: If the sheet becomes disabled while it was loading, and if it was either + // a main sheet or a sheet that was previously enabled via script, then we need + // to remove it from the list of pending sheets. + if (m_disabledState == 2 && (!m_alternate || oldDisabledState == 1)) + document()->removePendingSheet(); + + // Check #2: An alternate sheet becomes enabled while it is still loading. + if (m_alternate && m_disabledState == 1) + document()->addPendingSheet(); + + // Check #3: A main sheet becomes enabled while it was still loading and + // after it was disabled via script. It takes really terrible code to make this + // happen (a double toggle for no reason essentially). This happens on + // virtualplastic.net, which manages to do about 12 enable/disables on only 3 + // sheets. :) + if (!m_alternate && m_disabledState == 1 && oldDisabledState == 2) + document()->addPendingSheet(); + + // If the sheet is already loading just bail. + return; + } + + // Load the sheet, since it's never been loaded before. + if (!m_sheet && m_disabledState == 1) + process(); + else + document()->updateStyleSelector(); // Update the style selector. + } +} + +StyleSheet* HTMLLinkElement::sheet() const +{ + return m_sheet.get(); +} + +void HTMLLinkElement::parseMappedAttribute(MappedAttribute *attr) +{ + if (attr->name() == relAttr) { + tokenizeRelAttribute(attr->value(), m_isStyleSheet, m_alternate, m_isIcon); + process(); + } else if (attr->name() == hrefAttr) { + m_url = document()->completeURL(parseURL(attr->value())).string(); + process(); + } else if (attr->name() == typeAttr) { + m_type = attr->value(); + process(); + } else if (attr->name() == mediaAttr) { + m_media = attr->value().string().lower(); + process(); + } else if (attr->name() == disabledAttr) { + setDisabledState(!attr->isNull()); + } else { + if (attr->name() == titleAttr && m_sheet) + m_sheet->setTitle(attr->value()); + HTMLElement::parseMappedAttribute(attr); + } +} + +void HTMLLinkElement::tokenizeRelAttribute(const AtomicString& rel, bool& styleSheet, bool& alternate, bool& icon) +{ + styleSheet = false; + icon = false; + alternate = false; + if (equalIgnoringCase(rel, "stylesheet")) + styleSheet = true; + else if (equalIgnoringCase(rel, "icon") || equalIgnoringCase(rel, "shortcut icon")) + icon = true; + else if (equalIgnoringCase(rel, "alternate stylesheet") || equalIgnoringCase(rel, "stylesheet alternate")) { + styleSheet = true; + alternate = true; + } else { + // Tokenize the rel attribute and set bits based on specific keywords that we find. + String relString = rel.string(); + relString.replace('\n', ' '); + Vector<String> list; + relString.split(' ', list); + Vector<String>::const_iterator end = list.end(); + for (Vector<String>::const_iterator it = list.begin(); it != end; ++it) { + if (equalIgnoringCase(*it, "stylesheet")) + styleSheet = true; + else if (equalIgnoringCase(*it, "alternate")) + alternate = true; + else if (equalIgnoringCase(*it, "icon")) + icon = true; + } + } +} + +void HTMLLinkElement::process() +{ + if (!inDocument()) + return; + + String type = m_type.lower(); + + // IE extension: location of small icon for locationbar / bookmarks + // We'll record this URL per document, even if we later only use it in top level frames + if (m_isIcon && !m_url.isEmpty()) + document()->setIconURL(m_url, type); + + if (m_isDNSPrefetch && !m_url.isEmpty()) + prefetchDNS(KURL(m_url).host()); + + // Stylesheet + // This was buggy and would incorrectly match <link rel="alternate">, which has a different specified meaning. -dwh + if (m_disabledState != 2 && m_isStyleSheet && document()->frame()) { + // no need to load style sheets which aren't for the screen output + // ### there may be in some situations e.g. for an editor or script to manipulate + // also, don't load style sheets for standalone documents + MediaQueryEvaluator allEval(true); + MediaQueryEvaluator screenEval("screen", true); + MediaQueryEvaluator printEval("print", true); + RefPtr<MediaList> media = MediaList::createAllowingDescriptionSyntax(m_media); + if (allEval.eval(media.get()) || screenEval.eval(media.get()) || printEval.eval(media.get())) { + + // Add ourselves as a pending sheet, but only if we aren't an alternate + // stylesheet. Alternate stylesheets don't hold up render tree construction. + if (!isAlternate()) + document()->addPendingSheet(); + + String chset = getAttribute(charsetAttr); + if (chset.isEmpty() && document()->frame()) + chset = document()->frame()->loader()->encoding(); + + if (m_cachedSheet) { + if (m_loading) + document()->removePendingSheet(); + m_cachedSheet->removeClient(this); + } + m_loading = true; + m_cachedSheet = document()->docLoader()->requestCSSStyleSheet(m_url, chset); + if (m_cachedSheet) + m_cachedSheet->addClient(this); + else if (!isAlternate()) { // request may have been denied if stylesheet is local and document is remote. + m_loading = false; + document()->removePendingSheet(); + } + } + } else if (m_sheet) { + // we no longer contain a stylesheet, e.g. perhaps rel or type was changed + m_sheet = 0; + document()->updateStyleSelector(); + } +} + +void HTMLLinkElement::insertedIntoDocument() +{ + HTMLElement::insertedIntoDocument(); + document()->addStyleSheetCandidateNode(this, m_createdByParser); + process(); +} + +void HTMLLinkElement::removedFromDocument() +{ + HTMLElement::removedFromDocument(); + + // FIXME: It's terrible to do a synchronous update of the style selector just because a <style> or <link> element got removed. + if (document()->renderer()) { + document()->removeStyleSheetCandidateNode(this); + document()->updateStyleSelector(); + } +} + +void HTMLLinkElement::finishParsingChildren() +{ + m_createdByParser = false; + HTMLElement::finishParsingChildren(); +} + +void HTMLLinkElement::setCSSStyleSheet(const String& url, const String& charset, const CachedCSSStyleSheet* sheet) +{ + m_sheet = CSSStyleSheet::create(this, url, charset); + + bool strictParsing = !document()->inCompatMode(); + bool enforceMIMEType = strictParsing; + + // Check to see if we should enforce the MIME type of the CSS resource in strict mode. + // Running in iWeb 2 is one example of where we don't want to - <rdar://problem/6099748> + if (enforceMIMEType && document()->page() && !document()->page()->settings()->enforceCSSMIMETypeInStrictMode()) + enforceMIMEType = false; + + m_sheet->parseString(sheet->sheetText(enforceMIMEType), strictParsing); + m_sheet->setTitle(title()); + + RefPtr<MediaList> media = MediaList::createAllowingDescriptionSyntax(m_media); + m_sheet->setMedia(media.get()); + + m_loading = false; + m_sheet->checkLoaded(); +} + +bool HTMLLinkElement::isLoading() const +{ + if (m_loading) + return true; + if (!m_sheet) + return false; + return static_cast<CSSStyleSheet *>(m_sheet.get())->isLoading(); +} + +bool HTMLLinkElement::sheetLoaded() +{ + if (!isLoading() && !isDisabled() && !isAlternate()) { + document()->removePendingSheet(); + return true; + } + return false; +} + +bool HTMLLinkElement::isURLAttribute(Attribute *attr) const +{ + return attr->name() == hrefAttr; +} + +bool HTMLLinkElement::disabled() const +{ + return !getAttribute(disabledAttr).isNull(); +} + +void HTMLLinkElement::setDisabled(bool disabled) +{ + setAttribute(disabledAttr, disabled ? "" : 0); +} + +String HTMLLinkElement::charset() const +{ + return getAttribute(charsetAttr); +} + +void HTMLLinkElement::setCharset(const String& value) +{ + setAttribute(charsetAttr, value); +} + +KURL HTMLLinkElement::href() const +{ + return document()->completeURL(getAttribute(hrefAttr)); +} + +void HTMLLinkElement::setHref(const String& value) +{ + setAttribute(hrefAttr, value); +} + +String HTMLLinkElement::hreflang() const +{ + return getAttribute(hreflangAttr); +} + +void HTMLLinkElement::setHreflang(const String& value) +{ + setAttribute(hreflangAttr, value); +} + +String HTMLLinkElement::media() const +{ + return getAttribute(mediaAttr); +} + +void HTMLLinkElement::setMedia(const String& value) +{ + setAttribute(mediaAttr, value); +} + +String HTMLLinkElement::rel() const +{ + return getAttribute(relAttr); +} + +void HTMLLinkElement::setRel(const String& value) +{ + setAttribute(relAttr, value); +} + +String HTMLLinkElement::rev() const +{ + return getAttribute(revAttr); +} + +void HTMLLinkElement::setRev(const String& value) +{ + setAttribute(revAttr, value); +} + +String HTMLLinkElement::target() const +{ + return getAttribute(targetAttr); +} + +void HTMLLinkElement::setTarget(const String& value) +{ + setAttribute(targetAttr, value); +} + +String HTMLLinkElement::type() const +{ + return getAttribute(typeAttr); +} + +void HTMLLinkElement::setType(const String& value) +{ + setAttribute(typeAttr, value); +} + +void HTMLLinkElement::getSubresourceAttributeStrings(Vector<String>& urls) const +{ + if (m_isIcon) { + urls.append(href().string()); + return; + } + + if (!m_isStyleSheet) + return; + + // Append the URL of this link element. + urls.append(href().string()); + + // Walk the URLs linked by the linked-to stylesheet. + HashSet<String> styleURLs; + StyleSheet* styleSheet = const_cast<HTMLLinkElement*>(this)->sheet(); + if (styleSheet) + styleSheet->addSubresourceURLStrings(styleURLs, href()); + + HashSet<String>::iterator end = styleURLs.end(); + for (HashSet<String>::iterator i = styleURLs.begin(); i != end; ++i) + urls.append(*i); +} + +} diff --git a/webkit/pending/HTMLLinkElement.h b/webkit/pending/HTMLLinkElement.h new file mode 100644 index 0000000..9e58c18 --- /dev/null +++ b/webkit/pending/HTMLLinkElement.h @@ -0,0 +1,119 @@ +/* + * Copyright (C) 1999 Lars Knoll (knoll@kde.org) + * (C) 1999 Antti Koivisto (koivisto@kde.org) + * Copyright (C) 2003, 2008 Apple Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * + */ + +#ifndef HTMLLinkElement_h +#define HTMLLinkElement_h + +#include "CSSStyleSheet.h" +#include "CachedResourceClient.h" +#include "HTMLElement.h" + +namespace WebCore { + +class CachedCSSStyleSheet; +class KURL; + +class HTMLLinkElement : public HTMLElement, public CachedResourceClient { +public: + HTMLLinkElement(Document*); + ~HTMLLinkElement(); + + virtual HTMLTagStatus endTagRequirement() const { return TagStatusForbidden; } + virtual int tagPriority() const { return 0; } + + bool disabled() const; + void setDisabled(bool); + + String charset() const; + void setCharset(const String&); + + KURL href() const; + void setHref(const String&); + + String hreflang() const; + void setHreflang(const String&); + + String media() const; + void setMedia(const String&); + + String rel() const; + void setRel(const String&); + + String rev() const; + void setRev(const String&); + + virtual String target() const; + void setTarget(const String&); + + String type() const; + void setType(const String&); + + StyleSheet* sheet() const; + + // overload from HTMLElement + virtual void parseMappedAttribute(MappedAttribute*); + + void process(); + + virtual void insertedIntoDocument(); + virtual void removedFromDocument(); + + // from CachedResourceClient + virtual void setCSSStyleSheet(const String &url, const String& charset, const CachedCSSStyleSheet* sheet); + bool isLoading() const; + virtual bool sheetLoaded(); + + bool isAlternate() const { return m_disabledState == 0 && m_alternate; } + bool isDisabled() const { return m_disabledState == 2; } + bool isEnabledViaScript() const { return m_disabledState == 1; } + bool isIcon() const { return m_isIcon; } + + int disabledState() { return m_disabledState; } + void setDisabledState(bool _disabled); + + virtual bool isURLAttribute(Attribute*) const; + + static void tokenizeRelAttribute(const AtomicString& value, bool& stylesheet, bool& alternate, bool& icon); + + virtual void getSubresourceAttributeStrings(Vector<String>&) const; + + void setCreatedByParser(bool createdByParser) { m_createdByParser = createdByParser; } + virtual void finishParsingChildren(); + +protected: + CachedCSSStyleSheet* m_cachedSheet; + RefPtr<CSSStyleSheet> m_sheet; + String m_url; + String m_type; + String m_media; + int m_disabledState; // 0=unset(default), 1=enabled via script, 2=disabled + bool m_loading; + bool m_alternate; + bool m_isStyleSheet; + bool m_isIcon; + bool m_isDNSPrefetch; + bool m_createdByParser; +}; + +} //namespace + +#endif diff --git a/webkit/pending/String.cpp b/webkit/pending/String.cpp new file mode 100644 index 0000000..fe079cd --- /dev/null +++ b/webkit/pending/String.cpp @@ -0,0 +1,845 @@ +/* + * (C) 1999 Lars Knoll (knoll@kde.org) + * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "config.h" +#include "PlatformString.h" + +#include "CString.h" +#include "FloatConversion.h" +#include "StringBuffer.h" +#include "TextEncoding.h" +#include <kjs/dtoa.h> +#include <limits> +#include <stdarg.h> +#include <wtf/ASCIICType.h> +#include <wtf/StringExtras.h> +#include <wtf/Vector.h> +#include <wtf/unicode/Unicode.h> +#include <wtf/unicode/UTF8.h> + +#if USE(JSC) +using KJS::Identifier; +using KJS::UString; +#endif + +using namespace WTF; +using namespace WTF::Unicode; + +namespace WebCore { + +String::String(const UChar* str, unsigned len) +{ + if (!str) + return; + m_impl = StringImpl::create(str, len); +} + +String::String(const UChar* str) +{ + if (!str) + return; + + int len = 0; + while (str[len] != UChar(0)) + len++; + + m_impl = StringImpl::create(str, len); +} + +String::String(const char* str) +{ + if (!str) + return; + m_impl = StringImpl::create(str); +} + +String::String(const char* str, unsigned length) +{ + if (!str) + return; + m_impl = StringImpl::create(str, length); +} + +void String::append(const String& str) +{ + // FIXME: This is extremely inefficient. So much so that we might want to take this + // out of String's API. We can make it better by optimizing the case where exactly + // one String is pointing at this StringImpl, but even then it's going to require a + // call to fastMalloc every single time. + if (str.m_impl) { + if (m_impl) { + StringBuffer buffer(m_impl->length() + str.length()); + memcpy(buffer.characters(), m_impl->characters(), m_impl->length() * sizeof(UChar)); + memcpy(buffer.characters() + m_impl->length(), str.characters(), str.length() * sizeof(UChar)); + m_impl = StringImpl::adopt(buffer); + } else + m_impl = str.m_impl; + } +} + +void String::append(char c) +{ + // FIXME: This is extremely inefficient. So much so that we might want to take this + // out of String's API. We can make it better by optimizing the case where exactly + // one String is pointing at this StringImpl, but even then it's going to require a + // call to fastMalloc every single time. + if (m_impl) { + StringBuffer buffer(m_impl->length() + 1); + memcpy(buffer.characters(), m_impl->characters(), m_impl->length() * sizeof(UChar)); + buffer[m_impl->length()] = c; + m_impl = StringImpl::adopt(buffer); + } else + m_impl = StringImpl::create(&c, 1); +} + +void String::append(UChar c) +{ + // FIXME: This is extremely inefficient. So much so that we might want to take this + // out of String's API. We can make it better by optimizing the case where exactly + // one String is pointing at this StringImpl, but even then it's going to require a + // call to fastMalloc every single time. + if (m_impl) { + StringBuffer buffer(m_impl->length() + 1); + memcpy(buffer.characters(), m_impl->characters(), m_impl->length() * sizeof(UChar)); + buffer[m_impl->length()] = c; + m_impl = StringImpl::adopt(buffer); + } else + m_impl = StringImpl::create(&c, 1); +} + +String operator+(const String& a, const String& b) +{ + if (a.isEmpty()) + return b; + if (b.isEmpty()) + return a; + String c = a; + c += b; + return c; +} + +String operator+(const String& s, const char* cs) +{ + return s + String(cs); +} + +String operator+(const char* cs, const String& s) +{ + return String(cs) + s; +} + +void String::insert(const String& str, unsigned pos) +{ + if (str.isEmpty()) { + if (str.isNull()) + return; + if (isNull()) + m_impl = str.impl(); + return; + } + insert(str.characters(), str.length(), pos); +} + +void String::append(const UChar* charactersToAppend, unsigned lengthToAppend) +{ + if (!m_impl) { + if (!charactersToAppend) + return; + m_impl = StringImpl::create(charactersToAppend, lengthToAppend); + return; + } + + if (!lengthToAppend) + return; + + ASSERT(charactersToAppend); + StringBuffer buffer(length() + lengthToAppend); + memcpy(buffer.characters(), characters(), length() * sizeof(UChar)); + memcpy(buffer.characters() + length(), charactersToAppend, lengthToAppend * sizeof(UChar)); + m_impl = StringImpl::adopt(buffer); +} + +void String::insert(const UChar* charactersToInsert, unsigned lengthToInsert, unsigned position) +{ + if (position >= length()) { + append(charactersToInsert, lengthToInsert); + return; + } + + ASSERT(m_impl); + + if (!lengthToInsert) + return; + + ASSERT(charactersToInsert); + StringBuffer buffer(length() + lengthToInsert); + memcpy(buffer.characters(), characters(), position * sizeof(UChar)); + memcpy(buffer.characters() + position, charactersToInsert, lengthToInsert * sizeof(UChar)); + memcpy(buffer.characters() + position + lengthToInsert, characters() + position, (length() - position) * sizeof(UChar)); + m_impl = StringImpl::adopt(buffer); +} + +UChar String::operator[](unsigned i) const +{ + if (!m_impl || i >= m_impl->length()) + return 0; + return m_impl->characters()[i]; +} + +UChar32 String::characterStartingAt(unsigned i) const +{ + if (!m_impl || i >= m_impl->length()) + return 0; + return m_impl->characterStartingAt(i); +} + +unsigned String::length() const +{ + if (!m_impl) + return 0; + return m_impl->length(); +} + +void String::truncate(unsigned position) +{ + if (position >= length()) + return; + StringBuffer buffer(position); + memcpy(buffer.characters(), characters(), position * sizeof(UChar)); + m_impl = StringImpl::adopt(buffer); +} + +void String::remove(unsigned position, int lengthToRemove) +{ + if (lengthToRemove <= 0) + return; + if (position >= length()) + return; + if (static_cast<unsigned>(lengthToRemove) > length() - position) + lengthToRemove = length() - position; + StringBuffer buffer(length() - lengthToRemove); + memcpy(buffer.characters(), characters(), position * sizeof(UChar)); + memcpy(buffer.characters() + position, characters() + position + lengthToRemove, + (length() - lengthToRemove - position) * sizeof(UChar)); + m_impl = StringImpl::adopt(buffer); +} + +String String::substring(unsigned pos, unsigned len) const +{ + if (!m_impl) + return String(); + return m_impl->substring(pos, len); +} + +String String::lower() const +{ + if (!m_impl) + return String(); + return m_impl->lower(); +} + +String String::upper() const +{ + if (!m_impl) + return String(); + return m_impl->upper(); +} + +String String::stripWhiteSpace() const +{ + if (!m_impl) + return String(); + return m_impl->stripWhiteSpace(); +} + +String String::simplifyWhiteSpace() const +{ + if (!m_impl) + return String(); + return m_impl->simplifyWhiteSpace(); +} + +String String::foldCase() const +{ + if (!m_impl) + return String(); + return m_impl->foldCase(); +} + +bool String::percentage(int& result) const +{ + if (!m_impl || !m_impl->length()) + return false; + + if ((*m_impl)[m_impl->length() - 1] != '%') + return false; + + result = charactersToIntStrict(m_impl->characters(), m_impl->length() - 1); + return true; +} + +const UChar* String::characters() const +{ + if (!m_impl) + return 0; + return m_impl->characters(); +} + +const UChar* String::charactersWithNullTermination() +{ + if (!m_impl) + return 0; + if (m_impl->hasTerminatingNullCharacter()) + return m_impl->characters(); + m_impl = StringImpl::createWithTerminatingNullCharacter(*m_impl); + return m_impl->characters(); +} + +String String::format(const char *format, ...) +{ +#if PLATFORM(QT) + // Use QString::vsprintf to avoid the locale dependent formatting of vsnprintf. + // https://bugs.webkit.org/show_bug.cgi?id=18994 + va_list args; + va_start(args, format); + + QString buffer; + buffer.vsprintf(format, args); + + va_end(args); + + return buffer; +#else + va_list args; + va_start(args, format); + + Vector<char, 256> buffer; + + // Do the format once to get the length. +#if COMPILER(MSVC) + int result = _vscprintf(format, args); +#else + char ch; + int result = vsnprintf(&ch, 1, format, args); + // We need to call va_end() and then va_start() again here, as the + // contents of args is undefined after the call to vsnprintf + // according to http://man.cx/snprintf(3) + // + // Not calling va_end/va_start here happens to work on lots of + // systems, but fails e.g. on 64bit Linux. + va_end(args); + va_start(args, format); +#endif + + if (result == 0) + return String(""); + if (result < 0) + return String(); + unsigned len = result; + buffer.grow(len + 1); + + // Now do the formatting again, guaranteed to fit. + vsnprintf(buffer.data(), buffer.size(), format, args); + + va_end(args); + + return StringImpl::create(buffer.data(), len); +#endif +} + +String String::number(int n) +{ + return String::format("%d", n); +} + +String String::number(unsigned n) +{ + return String::format("%u", n); +} + +String String::number(long n) +{ + return String::format("%ld", n); +} + +String String::number(unsigned long n) +{ + return String::format("%lu", n); +} + +String String::number(long long n) +{ +#if PLATFORM(WIN_OS) + return String::format("%I64i", n); +#else + return String::format("%lli", n); +#endif +} + +String String::number(unsigned long long n) +{ +#if PLATFORM(WIN_OS) + return String::format("%I64u", n); +#else + return String::format("%llu", n); +#endif +} + +String String::number(double n) +{ + return String::format("%.6lg", n); +} + +int String::toIntStrict(bool* ok, int base) const +{ + if (!m_impl) { + if (ok) + *ok = false; + return 0; + } + return m_impl->toIntStrict(ok, base); +} + +unsigned String::toUIntStrict(bool* ok, int base) const +{ + if (!m_impl) { + if (ok) + *ok = false; + return 0; + } + return m_impl->toUIntStrict(ok, base); +} + +int64_t String::toInt64Strict(bool* ok, int base) const +{ + if (!m_impl) { + if (ok) + *ok = false; + return 0; + } + return m_impl->toInt64Strict(ok, base); +} + +uint64_t String::toUInt64Strict(bool* ok, int base) const +{ + if (!m_impl) { + if (ok) + *ok = false; + return 0; + } + return m_impl->toUInt64Strict(ok, base); +} + +int String::toInt(bool* ok) const +{ + if (!m_impl) { + if (ok) + *ok = false; + return 0; + } + return m_impl->toInt(ok); +} + +unsigned String::toUInt(bool* ok) const +{ + if (!m_impl) { + if (ok) + *ok = false; + return 0; + } + return m_impl->toUInt(ok); +} + +int64_t String::toInt64(bool* ok) const +{ + if (!m_impl) { + if (ok) + *ok = false; + return 0; + } + return m_impl->toInt64(ok); +} + +uint64_t String::toUInt64(bool* ok) const +{ + if (!m_impl) { + if (ok) + *ok = false; + return 0; + } + return m_impl->toUInt64(ok); +} + +double String::toDouble(bool* ok) const +{ + if (!m_impl) { + if (ok) + *ok = false; + return 0.0; + } + return m_impl->toDouble(ok); +} + +float String::toFloat(bool* ok) const +{ + if (!m_impl) { + if (ok) + *ok = false; + return 0.0f; + } + return m_impl->toFloat(ok); +} + +String String::copy() const +{ + if (!m_impl) + return String(); + return m_impl->copy(); +} + +bool String::isEmpty() const +{ + return !m_impl || !m_impl->length(); +} + +Length* String::toCoordsArray(int& len) const +{ + return m_impl ? m_impl->toCoordsArray(len) : 0; +} + +Length* String::toLengthArray(int& len) const +{ + return m_impl ? m_impl->toLengthArray(len) : 0; +} + +void String::split(const String& separator, bool allowEmptyEntries, Vector<String>& result) const +{ + result.clear(); + + int startPos = 0; + int endPos; + while ((endPos = find(separator, startPos)) != -1) { + if (allowEmptyEntries || startPos != endPos) + result.append(substring(startPos, endPos - startPos)); + startPos = endPos + separator.length(); + } + if (allowEmptyEntries || startPos != static_cast<int>(length())) + result.append(substring(startPos)); +} + +void String::split(const String& separator, Vector<String>& result) const +{ + return split(separator, false, result); +} + +void String::split(UChar separator, bool allowEmptyEntries, Vector<String>& result) const +{ + result.clear(); + + int startPos = 0; + int endPos; + while ((endPos = find(separator, startPos)) != -1) { + if (allowEmptyEntries || startPos != endPos) + result.append(substring(startPos, endPos - startPos)); + startPos = endPos + 1; + } + if (allowEmptyEntries || startPos != static_cast<int>(length())) + result.append(substring(startPos)); +} + +void String::split(UChar separator, Vector<String>& result) const +{ + return split(String(&separator, 1), false, result); +} + +#ifndef NDEBUG +Vector<char> String::ascii() const +{ + if (m_impl) + return m_impl->ascii(); + + const char* nullMsg = "(null impl)"; + Vector<char, 2048> buffer; + for (int i = 0; nullMsg[i]; ++i) + buffer.append(nullMsg[i]); + + buffer.append('\0'); + return buffer; +} +#endif + +CString String::latin1() const +{ + return Latin1Encoding().encode(characters(), length(), QuestionMarksForUnencodables); +} + +CString String::utf8() const +{ + return UTF8Encoding().encode(characters(), length(), QuestionMarksForUnencodables); +} + +String String::fromUTF8(const char* string, size_t size) +{ + if (!string) + return String(); + return UTF8Encoding().decode(string, size); +} + +String String::fromUTF8(const char* string) +{ + if (!string) + return String(); + return UTF8Encoding().decode(string, strlen(string)); +} + +#if USE(JSC) +String::String(const Identifier& str) +{ + if (str.isNull()) + return; + m_impl = StringImpl::create(str.data(), str.size()); +} + +String::String(const UString& str) +{ + if (str.isNull()) + return; + m_impl = StringImpl::create(str.data(), str.size()); +} + +String::operator UString() const +{ + if (!m_impl) + return UString(); + return UString(m_impl->characters(), m_impl->length()); +} +#endif + +// String Operations + +static bool isCharacterAllowedInBase(UChar c, int base) +{ + if (c > 0x7F) + return false; + if (isASCIIDigit(c)) + return c - '0' < base; + if (isASCIIAlpha(c)) { + if (base > 36) + base = 36; + return (c >= 'a' && c < 'a' + base - 10) + || (c >= 'A' && c < 'A' + base - 10); + } + return false; +} + +template <typename IntegralType> +static inline IntegralType toIntegralType(const UChar* data, size_t length, bool* ok, int base) +{ + static const IntegralType integralMax = std::numeric_limits<IntegralType>::max(); + static const bool isSigned = std::numeric_limits<IntegralType>::is_signed; + const IntegralType maxMultiplier = integralMax / base; + + IntegralType value = 0; + bool isOk = false; + bool isNegative = false; + + if (!data) + goto bye; + + // skip leading whitespace + while (length && isSpaceOrNewline(*data)) { + length--; + data++; + } + + if (isSigned && length && *data == '-') { + length--; + data++; + isNegative = true; + } else if (length && *data == '+') { + length--; + data++; + } + + if (!length || !isCharacterAllowedInBase(*data, base)) + goto bye; + + while (length && isCharacterAllowedInBase(*data, base)) { + length--; + IntegralType digitValue; + UChar c = *data; + if (isASCIIDigit(c)) + digitValue = c - '0'; + else if (c >= 'a') + digitValue = c - 'a' + 10; + else + digitValue = c - 'A' + 10; + + if (value > maxMultiplier || (value == maxMultiplier && digitValue > (integralMax % base) + isNegative)) + goto bye; + + value = base * value + digitValue; + data++; + } + +#if COMPILER(MSVC) +#pragma warning(push, 0) +#pragma warning(disable:4146) +#endif + + if (isNegative) + value = -value; + +#if COMPILER(MSVC) +#pragma warning(pop) +#endif + + // skip trailing space + while (length && isSpaceOrNewline(*data)) { + length--; + data++; + } + + if (!length) + isOk = true; +bye: + if (ok) + *ok = isOk; + return isOk ? value : 0; +} + +static unsigned lengthOfCharactersAsInteger(const UChar* data, size_t length) +{ + size_t i = 0; + + // Allow leading spaces. + for (; i != length; ++i) { + if (!isSpaceOrNewline(data[i])) + break; + } + + // Allow sign. + if (i != length && (data[i] == '+' || data[i] == '-')) + ++i; + + // Allow digits. + for (; i != length; ++i) { + if (!Unicode::isDigit(data[i])) + break; + } + + return i; +} + +int charactersToIntStrict(const UChar* data, size_t length, bool* ok, int base) +{ + return toIntegralType<int>(data, length, ok, base); +} + +unsigned charactersToUIntStrict(const UChar* data, size_t length, bool* ok, int base) +{ + return toIntegralType<unsigned>(data, length, ok, base); +} + +int64_t charactersToInt64Strict(const UChar* data, size_t length, bool* ok, int base) +{ + return toIntegralType<int64_t>(data, length, ok, base); +} + +uint64_t charactersToUInt64Strict(const UChar* data, size_t length, bool* ok, int base) +{ + return toIntegralType<uint64_t>(data, length, ok, base); +} + +int charactersToInt(const UChar* data, size_t length, bool* ok) +{ + return toIntegralType<int>(data, lengthOfCharactersAsInteger(data, length), ok, 10); +} + +unsigned charactersToUInt(const UChar* data, size_t length, bool* ok) +{ + return toIntegralType<unsigned>(data, lengthOfCharactersAsInteger(data, length), ok, 10); +} + +int64_t charactersToInt64(const UChar* data, size_t length, bool* ok) +{ + return toIntegralType<int64_t>(data, lengthOfCharactersAsInteger(data, length), ok, 10); +} + +uint64_t charactersToUInt64(const UChar* data, size_t length, bool* ok) +{ + return toIntegralType<uint64_t>(data, lengthOfCharactersAsInteger(data, length), ok, 10); +} + +double charactersToDouble(const UChar* data, size_t length, bool* ok) +{ + if (!length) { + if (ok) + *ok = false; + return 0.0; + } + + Vector<char, 256> bytes(length + 1); + for (unsigned i = 0; i < length; ++i) + bytes[i] = data[i] < 0x7F ? data[i] : '?'; + bytes[length] = '\0'; + char* end; +#if USE(JSC) + double val = KJS::strtod(bytes.data(), &end); +#elif USE(V8) + double val = strtod(bytes.data(), &end); +#endif + if (ok) + *ok = (end == 0 || *end == '\0'); + return val; +} + +float charactersToFloat(const UChar* data, size_t length, bool* ok) +{ + // FIXME: This will return ok even when the string fits into a double but not a float. + return narrowPrecisionToFloat(charactersToDouble(data, length, ok)); +} + +PassRefPtr<SharedBuffer> utf8Buffer(const String& string) +{ + // Allocate a buffer big enough to hold all the characters. + const int length = string.length(); + Vector<char> buffer(length * 3); + + // Convert to runs of 8-bit characters. + char* p = buffer.data(); + const UChar* d = string.characters(); + ConversionResult result = convertUTF16ToUTF8(&d, d + length, &p, p + buffer.size(), true); + if (result != conversionOK) + return 0; + + buffer.shrink(p - buffer.data()); + return SharedBuffer::adoptVector(buffer); +} + +} // namespace WebCore + +#ifndef NDEBUG +// For debugging only -- leaks memory +WebCore::String* string(const char* s) +{ + return new WebCore::String(s); +} +#endif diff --git a/webkit/pending/StringImpl.cpp b/webkit/pending/StringImpl.cpp new file mode 100644 index 0000000..83fb984 --- /dev/null +++ b/webkit/pending/StringImpl.cpp @@ -0,0 +1,1051 @@ +/* + * Copyright (C) 1999 Lars Knoll (knoll@kde.org) + * (C) 1999 Antti Koivisto (koivisto@kde.org) + * (C) 2001 Dirk Mueller ( mueller@kde.org ) + * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. + * Copyright (C) 2006 Andrew Wellington (proton@wiretapped.net) + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * + */ + +#include "config.h" +#include "StringImpl.h" + +#include "AtomicString.h" +#include "CString.h" +#include "CharacterNames.h" +#include "FloatConversion.h" +#include "Length.h" +#include "StringBuffer.h" +#include "StringHash.h" +#include "TextBreakIterator.h" +#include "TextEncoding.h" +#include <kjs/dtoa.h> +#include <kjs/identifier.h> +#include <wtf/Assertions.h> +#include <wtf/unicode/Unicode.h> + +using namespace WTF; +using namespace Unicode; + +#if USE(JSC) +using KJS::Identifier; +using KJS::UString; +#endif + +namespace WebCore { + +static inline UChar* newUCharVector(unsigned n) +{ + return static_cast<UChar*>(fastMalloc(sizeof(UChar) * n)); +} + +static inline void deleteUCharVector(const UChar* p) +{ + fastFree(const_cast<UChar*>(p)); +} + +// This constructor is used only to create the empty string. +StringImpl::StringImpl() + : m_length(0) + , m_data(0) + , m_hash(0) + , m_inTable(false) + , m_hasTerminatingNullCharacter(false) +{ +} + +// This is one of the most common constructors, but it's also used for the copy() +// operation. Because of that, it's the one constructor that doesn't assert the +// length is non-zero, since we support copying the empty string. +inline StringImpl::StringImpl(const UChar* characters, unsigned length) + : m_length(length) + , m_hash(0) + , m_inTable(false) + , m_hasTerminatingNullCharacter(false) +{ + UChar* data = newUCharVector(length); + memcpy(data, characters, length * sizeof(UChar)); + m_data = data; +} + +inline StringImpl::StringImpl(const StringImpl& str, WithTerminatingNullCharacter) + : m_length(str.m_length) + , m_hash(str.m_hash) + , m_inTable(false) + , m_hasTerminatingNullCharacter(true) +{ + UChar* data = newUCharVector(str.m_length + 1); + memcpy(data, str.m_data, str.m_length * sizeof(UChar)); + data[str.m_length] = 0; + m_data = data; +} + +inline StringImpl::StringImpl(const char* characters, unsigned length) + : m_length(length) + , m_hash(0) + , m_inTable(false) + , m_hasTerminatingNullCharacter(false) +{ + ASSERT(characters); + ASSERT(length); + + UChar* data = newUCharVector(length); + for (unsigned i = 0; i != length; ++i) { + unsigned char c = characters[i]; + data[i] = c; + } + m_data = data; +} + +inline StringImpl::StringImpl(UChar* characters, unsigned length, AdoptBuffer) + : m_length(length) + , m_data(characters) + , m_hash(0) + , m_inTable(false) + , m_hasTerminatingNullCharacter(false) +{ + ASSERT(characters); + ASSERT(length); +} + +// This constructor is only for use by AtomicString. +StringImpl::StringImpl(const UChar* characters, unsigned length, unsigned hash) + : m_length(length) + , m_hash(hash) + , m_inTable(true) + , m_hasTerminatingNullCharacter(false) +{ + ASSERT(hash); + ASSERT(characters); + ASSERT(length); + + UChar* data = newUCharVector(length); + memcpy(data, characters, length * sizeof(UChar)); + m_data = data; +} + +// This constructor is only for use by AtomicString. +StringImpl::StringImpl(const char* characters, unsigned length, unsigned hash) + : m_length(length) + , m_hash(hash) + , m_inTable(true) + , m_hasTerminatingNullCharacter(false) +{ + ASSERT(hash); + ASSERT(characters); + ASSERT(length); + + UChar* data = newUCharVector(length); + for (unsigned i = 0; i != length; ++i) { + unsigned char c = characters[i]; + data[i] = c; + } + m_data = data; +} + +StringImpl::~StringImpl() +{ + if (m_inTable) + AtomicString::remove(this); + deleteUCharVector(m_data); +} + +StringImpl* StringImpl::empty() +{ + static StringImpl* e = new StringImpl; + return e; +} + +bool StringImpl::containsOnlyWhitespace() +{ + // FIXME: The definition of whitespace here includes a number of characters + // that are not whitespace from the point of view of RenderText; I wonder if + // that's a problem in practice. + for (unsigned i = 0; i < m_length; i++) + if (!isASCIISpace(m_data[i])) + return false; + return true; +} + +PassRefPtr<StringImpl> StringImpl::substring(unsigned pos, unsigned len) +{ + if (pos >= m_length) + return empty(); + if (len > m_length - pos) + len = m_length - pos; + return create(m_data + pos, len); +} + +UChar32 StringImpl::characterStartingAt(unsigned i) +{ + if (U16_IS_SINGLE(m_data[i])) + return m_data[i]; + if (i + 1 < m_length && U16_IS_LEAD(m_data[i]) && U16_IS_TRAIL(m_data[i + 1])) + return U16_GET_SUPPLEMENTARY(m_data[i], m_data[i + 1]); + return 0; +} + +static Length parseLength(const UChar* data, unsigned length) +{ + if (length == 0) + return Length(1, Relative); + + unsigned i = 0; + while (i < length && isSpaceOrNewline(data[i])) + ++i; + if (i < length && (data[i] == '+' || data[i] == '-')) + ++i; + while (i < length && Unicode::isDigit(data[i])) + ++i; + + bool ok; + int r = charactersToIntStrict(data, i, &ok); + + /* Skip over any remaining digits, we are not that accurate (5.5% => 5%) */ + while (i < length && (Unicode::isDigit(data[i]) || data[i] == '.')) + ++i; + + /* IE Quirk: Skip any whitespace (20 % => 20%) */ + while (i < length && isSpaceOrNewline(data[i])) + ++i; + + if (ok) { + if (i < length) { + UChar next = data[i]; + if (next == '%') + return Length(static_cast<double>(r), Percent); + if (next == '*') + return Length(r, Relative); + } + return Length(r, Fixed); + } else { + if (i < length) { + UChar next = data[i]; + if (next == '*') + return Length(1, Relative); + if (next == '%') + return Length(1, Relative); + } + } + return Length(0, Relative); +} + +Length StringImpl::toLength() +{ + return parseLength(m_data, m_length); +} + +static int countCharacter(StringImpl* string, UChar character) +{ + int count = 0; + int length = string->length(); + for (int i = 0; i < length; ++i) + count += (*string)[i] == character; + return count; +} + +Length* StringImpl::toCoordsArray(int& len) +{ + StringBuffer spacified(m_length); + for (unsigned i = 0; i < m_length; i++) { + UChar cc = m_data[i]; + if (cc > '9' || (cc < '0' && cc != '-' && cc != '*' && cc != '.')) + spacified[i] = ' '; + else + spacified[i] = cc; + } + RefPtr<StringImpl> str = adopt(spacified); + + str = str->simplifyWhiteSpace(); + + len = countCharacter(str.get(), ' ') + 1; + Length* r = new Length[len]; + + int i = 0; + int pos = 0; + int pos2; + + while ((pos2 = str->find(' ', pos)) != -1) { + r[i++] = parseLength(str->characters() + pos, pos2 - pos); + pos = pos2+1; + } + r[i] = parseLength(str->characters() + pos, str->length() - pos); + + ASSERT(i == len - 1); + + return r; +} + +Length* StringImpl::toLengthArray(int& len) +{ + RefPtr<StringImpl> str = simplifyWhiteSpace(); + if (!str->length()) { + len = 1; + return 0; + } + + len = countCharacter(str.get(), ',') + 1; + Length* r = new Length[len]; + + int i = 0; + int pos = 0; + int pos2; + + while ((pos2 = str->find(',', pos)) != -1) { + r[i++] = parseLength(str->characters() + pos, pos2 - pos); + pos = pos2+1; + } + + ASSERT(i == len - 1); + + /* IE Quirk: If the last comma is the last char skip it and reduce len by one */ + if (str->length()-pos > 0) + r[i] = parseLength(str->characters() + pos, str->length() - pos); + else + len--; + + return r; +} + +bool StringImpl::isLower() +{ + // Do a faster loop for the case where all the characters are ASCII. + bool allLower = true; + UChar ored = 0; + for (unsigned i = 0; i < m_length; i++) { + UChar c = m_data[i]; + allLower = allLower && isASCIILower(c); + ored |= c; + } + if (!(ored & ~0x7F)) + return allLower; + + // Do a slower check for cases that include non-ASCII characters. + allLower = true; + unsigned i = 0; + while (i < m_length) { + UChar32 character; + U16_NEXT(m_data, i, m_length, character) + allLower = allLower && Unicode::isLower(character); + } + return allLower; +} + +PassRefPtr<StringImpl> StringImpl::lower() +{ + StringBuffer data(m_length); + int32_t length = m_length; + + // Do a faster loop for the case where all the characters are ASCII. + UChar ored = 0; + for (int i = 0; i < length; i++) { + UChar c = m_data[i]; + ored |= c; + data[i] = toASCIILower(c); + } + if (!(ored & ~0x7F)) + return adopt(data); + + // Do a slower implementation for cases that include non-ASCII characters. + bool error; + int32_t realLength = Unicode::toLower(data.characters(), length, m_data, m_length, &error); + if (!error && realLength == length) + return adopt(data); + data.resize(realLength); + Unicode::toLower(data.characters(), realLength, m_data, m_length, &error); + if (error) + return this; + return adopt(data); +} + +PassRefPtr<StringImpl> StringImpl::upper() +{ + bool error; + int32_t length = Unicode::toUpper(0, 0, m_data, m_length, &error); + StringBuffer data(length); + Unicode::toUpper(data.characters(), length, m_data, m_length, &error); + if (error) + return this; + return adopt(data); +} + +PassRefPtr<StringImpl> StringImpl::secure(UChar aChar) +{ + int length = m_length; + StringBuffer data(length); + for (int i = 0; i < length; ++i) + data[i] = aChar; + return adopt(data); +} + +PassRefPtr<StringImpl> StringImpl::foldCase() +{ + StringBuffer data(m_length); + int32_t length = m_length; + + // Do a faster loop for the case where all the characters are ASCII. + UChar ored = 0; + for (int i = 0; i < length; i++) { + UChar c = m_data[i]; + ored |= c; + data[i] = toASCIILower(c); + } + if (!(ored & ~0x7F)) + return adopt(data); + + // Do a slower implementation for cases that include non-ASCII characters. + bool error; + int32_t realLength = Unicode::foldCase(data.characters(), length, m_data, m_length, &error); + if (!error && realLength == length) + return adopt(data); + data.resize(realLength); + Unicode::foldCase(data.characters(), realLength, m_data, m_length, &error); + if (error) + return this; + return adopt(data); +} + +PassRefPtr<StringImpl> StringImpl::stripWhiteSpace() +{ + if (!m_length) + return empty(); + + unsigned start = 0; + unsigned end = m_length - 1; + + // skip white space from start + while (start <= end && isSpaceOrNewline(m_data[start])) + start++; + + // only white space + if (start > end) + return empty(); + + // skip white space from end + while (end && isSpaceOrNewline(m_data[end])) + end--; + + return create(m_data + start, end + 1 - start); +} + +PassRefPtr<StringImpl> StringImpl::simplifyWhiteSpace() +{ + StringBuffer data(m_length); + + const UChar* from = m_data; + const UChar* fromend = from + m_length; + int outc = 0; + + UChar* to = data.characters(); + + while (true) { + while (from != fromend && isSpaceOrNewline(*from)) + from++; + while (from != fromend && !isSpaceOrNewline(*from)) + to[outc++] = *from++; + if (from != fromend) + to[outc++] = ' '; + else + break; + } + + if (outc > 0 && to[outc - 1] == ' ') + outc--; + + data.shrink(outc); + + return adopt(data); +} + +PassRefPtr<StringImpl> StringImpl::capitalize(UChar previous) +{ + StringBuffer stringWithPrevious(m_length + 1); + stringWithPrevious[0] = previous == noBreakSpace ? ' ' : previous; + for (unsigned i = 1; i < m_length + 1; i++) { + // Replace   with a real space since ICU no longer treats   as a word separator. + if (m_data[i - 1] == noBreakSpace) + stringWithPrevious[i] = ' '; + else + stringWithPrevious[i] = m_data[i - 1]; + } + + TextBreakIterator* boundary = wordBreakIterator(stringWithPrevious.characters(), m_length + 1); + if (!boundary) + return this; + + StringBuffer data(m_length); + + int32_t endOfWord; + int32_t startOfWord = textBreakFirst(boundary); + for (endOfWord = textBreakNext(boundary); endOfWord != TextBreakDone; startOfWord = endOfWord, endOfWord = textBreakNext(boundary)) { + if (startOfWord != 0) // Ignore first char of previous string + data[startOfWord - 1] = m_data[startOfWord - 1] == noBreakSpace ? noBreakSpace : toTitleCase(stringWithPrevious[startOfWord]); + for (int i = startOfWord + 1; i < endOfWord; i++) + data[i - 1] = m_data[i - 1]; + } + + return adopt(data); +} + +int StringImpl::toIntStrict(bool* ok, int base) +{ + return charactersToIntStrict(m_data, m_length, ok, base); +} + +unsigned StringImpl::toUIntStrict(bool* ok, int base) +{ + return charactersToUIntStrict(m_data, m_length, ok, base); +} + +int64_t StringImpl::toInt64Strict(bool* ok, int base) +{ + return charactersToInt64Strict(m_data, m_length, ok, base); +} + +uint64_t StringImpl::toUInt64Strict(bool* ok, int base) +{ + return charactersToUInt64Strict(m_data, m_length, ok, base); +} + +int StringImpl::toInt(bool* ok) +{ + return charactersToInt(m_data, m_length, ok); +} + +unsigned StringImpl::toUInt(bool* ok) +{ + return charactersToUInt(m_data, m_length, ok); +} + +int64_t StringImpl::toInt64(bool* ok) +{ + return charactersToInt64(m_data, m_length, ok); +} + +uint64_t StringImpl::toUInt64(bool* ok) +{ + return charactersToUInt64(m_data, m_length, ok); +} + +double StringImpl::toDouble(bool* ok) +{ + return charactersToDouble(m_data, m_length, ok); +} + +float StringImpl::toFloat(bool* ok) +{ + return charactersToFloat(m_data, m_length, ok); +} + +static bool equal(const UChar* a, const char* b, int length) +{ + ASSERT(length >= 0); + while (length--) { + unsigned char bc = *b++; + if (*a++ != bc) + return false; + } + return true; +} + +static bool equalIgnoringCase(const UChar* a, const char* b, int length) +{ + ASSERT(length >= 0); + while (length--) { + unsigned char bc = *b++; + if (foldCase(*a++) != foldCase(bc)) + return false; + } + return true; +} + +static inline bool equalIgnoringCase(const UChar* a, const UChar* b, int length) +{ + ASSERT(length >= 0); + return umemcasecmp(a, b, length) == 0; +} + +int StringImpl::find(const char* chs, int index, bool caseSensitive) +{ + if (!chs || index < 0) + return -1; + + int chsLength = strlen(chs); + int n = m_length - index; + if (n < 0) + return -1; + n -= chsLength - 1; + if (n <= 0) + return -1; + + const char* chsPlusOne = chs + 1; + int chsLengthMinusOne = chsLength - 1; + + const UChar* ptr = m_data + index - 1; + if (caseSensitive) { + UChar c = *chs; + do { + if (*++ptr == c && equal(ptr + 1, chsPlusOne, chsLengthMinusOne)) + return m_length - chsLength - n + 1; + } while (--n); + } else { + UChar lc = Unicode::foldCase(*chs); + do { + if (Unicode::foldCase(*++ptr) == lc && equalIgnoringCase(ptr + 1, chsPlusOne, chsLengthMinusOne)) + return m_length - chsLength - n + 1; + } while (--n); + } + + return -1; +} + +int StringImpl::find(UChar c, int start) +{ + return WebCore::find(m_data, m_length, c, start); +} + +int StringImpl::find(StringImpl* str, int index, bool caseSensitive) +{ + /* + We use a simple trick for efficiency's sake. Instead of + comparing strings, we compare the sum of str with that of + a part of this string. Only if that matches, we call memcmp + or ucstrnicmp. + */ + ASSERT(str); + if (index < 0) + index += m_length; + int lstr = str->m_length; + int lthis = m_length - index; + if ((unsigned)lthis > m_length) + return -1; + int delta = lthis - lstr; + if (delta < 0) + return -1; + + const UChar* uthis = m_data + index; + const UChar* ustr = str->m_data; + unsigned hthis = 0; + unsigned hstr = 0; + if (caseSensitive) { + for (int i = 0; i < lstr; i++) { + hthis += uthis[i]; + hstr += ustr[i]; + } + int i = 0; + while (1) { + if (hthis == hstr && memcmp(uthis + i, ustr, lstr * sizeof(UChar)) == 0) + return index + i; + if (i == delta) + return -1; + hthis += uthis[i + lstr]; + hthis -= uthis[i]; + i++; + } + } else { + for (int i = 0; i < lstr; i++ ) { + hthis += toASCIILower(uthis[i]); + hstr += toASCIILower(ustr[i]); + } + int i = 0; + while (1) { + if (hthis == hstr && equalIgnoringCase(uthis + i, ustr, lstr)) + return index + i; + if (i == delta) + return -1; + hthis += toASCIILower(uthis[i + lstr]); + hthis -= toASCIILower(uthis[i]); + i++; + } + } +} + +int StringImpl::reverseFind(UChar c, int index) +{ + return WebCore::reverseFind(m_data, m_length, c, index); +} + +int StringImpl::reverseFind(StringImpl* str, int index, bool caseSensitive) +{ + /* + See StringImpl::find() for explanations. + */ + ASSERT(str); + int lthis = m_length; + if (index < 0) + index += lthis; + + int lstr = str->m_length; + int delta = lthis - lstr; + if ( index < 0 || index > lthis || delta < 0 ) + return -1; + if ( index > delta ) + index = delta; + + const UChar *uthis = m_data; + const UChar *ustr = str->m_data; + unsigned hthis = 0; + unsigned hstr = 0; + int i; + if (caseSensitive) { + for ( i = 0; i < lstr; i++ ) { + hthis += uthis[index + i]; + hstr += ustr[i]; + } + i = index; + while (1) { + if (hthis == hstr && memcmp(uthis + i, ustr, lstr * sizeof(UChar)) == 0) + return i; + if (i == 0) + return -1; + i--; + hthis -= uthis[i + lstr]; + hthis += uthis[i]; + } + } else { + for (i = 0; i < lstr; i++) { + hthis += toASCIILower(uthis[index + i]); + hstr += toASCIILower(ustr[i]); + } + i = index; + while (1) { + if (hthis == hstr && equalIgnoringCase(uthis + i, ustr, lstr) ) + return i; + if (i == 0) + return -1; + i--; + hthis -= toASCIILower(uthis[i + lstr]); + hthis += toASCIILower(uthis[i]); + } + } + + // Should never get here. + return -1; +} + +bool StringImpl::endsWith(StringImpl* m_data, bool caseSensitive) +{ + ASSERT(m_data); + int start = m_length - m_data->m_length; + if (start >= 0) + return (find(m_data, start, caseSensitive) == start); + return false; +} + +PassRefPtr<StringImpl> StringImpl::replace(UChar oldC, UChar newC) +{ + if (oldC == newC) + return this; + unsigned i; + for (i = 0; i != m_length; ++i) + if (m_data[i] == oldC) + break; + if (i == m_length) + return this; + + StringBuffer data(m_length); + for (i = 0; i != m_length; ++i) { + UChar ch = m_data[i]; + if (ch == oldC) + ch = newC; + data[i] = ch; + } + return adopt(data); +} + +PassRefPtr<StringImpl> StringImpl::replace(unsigned position, unsigned lengthToReplace, StringImpl* str) +{ + position = min(position, length()); + lengthToReplace = min(lengthToReplace, length() - position); + unsigned lengthToInsert = str ? str->length() : 0; + if (!lengthToReplace && !lengthToInsert) + return this; + StringBuffer buffer(length() - lengthToReplace + lengthToInsert); + memcpy(buffer.characters(), characters(), position * sizeof(UChar)); + if (str) + memcpy(buffer.characters() + position, str->characters(), lengthToInsert * sizeof(UChar)); + memcpy(buffer.characters() + position + lengthToInsert, characters() + position + lengthToReplace, + (length() - position - lengthToReplace) * sizeof(UChar)); + return adopt(buffer); +} + +PassRefPtr<StringImpl> StringImpl::replace(UChar pattern, StringImpl* replacement) +{ + if (!replacement) + return this; + + int repStrLength = replacement->length(); + int srcSegmentStart = 0; + int matchCount = 0; + + // Count the matches + while ((srcSegmentStart = find(pattern, srcSegmentStart)) >= 0) { + ++matchCount; + ++srcSegmentStart; + } + + // If we have 0 matches, we don't have to do any more work + if (!matchCount) + return this; + + StringBuffer data(m_length - matchCount + (matchCount * repStrLength)); + + // Construct the new data + int srcSegmentEnd; + int srcSegmentLength; + srcSegmentStart = 0; + int dstOffset = 0; + + while ((srcSegmentEnd = find(pattern, srcSegmentStart)) >= 0) { + srcSegmentLength = srcSegmentEnd - srcSegmentStart; + memcpy(data.characters() + dstOffset, m_data + srcSegmentStart, srcSegmentLength * sizeof(UChar)); + dstOffset += srcSegmentLength; + memcpy(data.characters() + dstOffset, replacement->m_data, repStrLength * sizeof(UChar)); + dstOffset += repStrLength; + srcSegmentStart = srcSegmentEnd + 1; + } + + srcSegmentLength = m_length - srcSegmentStart; + memcpy(data.characters() + dstOffset, m_data + srcSegmentStart, srcSegmentLength * sizeof(UChar)); + + ASSERT(dstOffset + srcSegmentLength == static_cast<int>(data.length())); + + return adopt(data); +} + +PassRefPtr<StringImpl> StringImpl::replace(StringImpl* pattern, StringImpl* replacement) +{ + if (!pattern || !replacement) + return this; + + int patternLength = pattern->length(); + if (!patternLength) + return this; + + int repStrLength = replacement->length(); + int srcSegmentStart = 0; + int matchCount = 0; + + // Count the matches + while ((srcSegmentStart = find(pattern, srcSegmentStart)) >= 0) { + ++matchCount; + srcSegmentStart += patternLength; + } + + // If we have 0 matches, we don't have to do any more work + if (!matchCount) + return this; + + StringBuffer data(m_length + matchCount * (repStrLength - patternLength)); + + // Construct the new data + int srcSegmentEnd; + int srcSegmentLength; + srcSegmentStart = 0; + int dstOffset = 0; + + while ((srcSegmentEnd = find(pattern, srcSegmentStart)) >= 0) { + srcSegmentLength = srcSegmentEnd - srcSegmentStart; + memcpy(data.characters() + dstOffset, m_data + srcSegmentStart, srcSegmentLength * sizeof(UChar)); + dstOffset += srcSegmentLength; + memcpy(data.characters() + dstOffset, replacement->m_data, repStrLength * sizeof(UChar)); + dstOffset += repStrLength; + srcSegmentStart = srcSegmentEnd + patternLength; + } + + srcSegmentLength = m_length - srcSegmentStart; + memcpy(data.characters() + dstOffset, m_data + srcSegmentStart, srcSegmentLength * sizeof(UChar)); + + ASSERT(dstOffset + srcSegmentLength == static_cast<int>(data.length())); + + return adopt(data); +} + +bool equal(StringImpl* a, StringImpl* b) +{ + return StringHash::equal(a, b); +} + +bool equal(StringImpl* a, const char* b) +{ + if (!a) + return !b; + if (!b) + return !a; + + unsigned length = a->length(); + const UChar* as = a->characters(); + for (unsigned i = 0; i != length; ++i) { + unsigned char bc = b[i]; + if (!bc) + return false; + if (as[i] != bc) + return false; + } + + return !b[length]; +} + +bool equalIgnoringCase(StringImpl* a, StringImpl* b) +{ + return CaseFoldingHash::equal(a, b); +} + +bool equalIgnoringCase(StringImpl* a, const char* b) +{ + if (!a) + return !b; + if (!b) + return !a; + + unsigned length = a->length(); + const UChar* as = a->characters(); + + // Do a faster loop for the case where all the characters are ASCII. + UChar ored = 0; + bool equal = true; + for (unsigned i = 0; i != length; ++i) { + char bc = b[i]; + if (!bc) + return false; + UChar ac = as[i]; + ored |= ac; + equal = equal && (toASCIILower(ac) == toASCIILower(bc)); + } + + // Do a slower implementation for cases that include non-ASCII characters. + if (ored & ~0x7F) { + equal = true; + for (unsigned i = 0; i != length; ++i) { + unsigned char bc = b[i]; + equal = equal && (foldCase(as[i]) == foldCase(bc)); + } + } + + return equal && !b[length]; +} + +Vector<char> StringImpl::ascii() +{ + Vector<char> buffer(m_length + 1); + for (unsigned i = 0; i != m_length; ++i) { + UChar c = m_data[i]; + if ((c >= 0x20 && c < 0x7F) || c == 0x00) + buffer[i] = c; + else + buffer[i] = '?'; + } + buffer[m_length] = '\0'; + return buffer; +} + +WTF::Unicode::Direction StringImpl::defaultWritingDirection() +{ + for (unsigned i = 0; i < m_length; ++i) { + WTF::Unicode::Direction charDirection = WTF::Unicode::direction(m_data[i]); + if (charDirection == WTF::Unicode::LeftToRight) + return WTF::Unicode::LeftToRight; + if (charDirection == WTF::Unicode::RightToLeft || charDirection == WTF::Unicode::RightToLeftArabic) + return WTF::Unicode::RightToLeft; + } + return WTF::Unicode::LeftToRight; +} + +// This is a hot function because it's used when parsing HTML. +PassRefPtr<StringImpl> StringImpl::createStrippingNullCharacters(const UChar* characters, unsigned length) +{ + ASSERT(characters); + ASSERT(length); + + // Optimize for the case where there are no Null characters by quickly + // searching for nulls, and then using StringImpl::create, which will + // memcpy the whole buffer. This is faster than assigning character by + // character during the loop. + + // Fast case. + int foundNull = 0; + for (unsigned i = 0; !foundNull && i < length; i++) { + int c = characters[i]; // more efficient than using UChar here (at least on Intel Mac OS) + foundNull |= !c; + } + if (!foundNull) + return StringImpl::create(characters, length); + + // Slow case. + StringBuffer strippedCopy(length); + unsigned strippedLength = 0; + for (unsigned i = 0; i < length; i++) { + if (int c = characters[i]) + strippedCopy[strippedLength++] = c; + } + ASSERT(strippedLength < length); // Only take the slow case when stripping. + strippedCopy.shrink(strippedLength); + return adopt(strippedCopy); +} + +PassRefPtr<StringImpl> StringImpl::adopt(StringBuffer& buffer) +{ + unsigned length = buffer.length(); + if (length == 0) + return empty(); + return adoptRef(new StringImpl(buffer.release(), length, AdoptBuffer())); +} + +PassRefPtr<StringImpl> StringImpl::adopt(Vector<UChar>& vector) +{ + size_t size = vector.size(); + if (size == 0) + return empty(); + return adoptRef(new StringImpl(vector.releaseBuffer(), size, AdoptBuffer())); +} + +PassRefPtr<StringImpl> StringImpl::create(const UChar* characters, unsigned length) +{ + if (!characters || !length) + return empty(); + return adoptRef(new StringImpl(characters, length)); +} + +PassRefPtr<StringImpl> StringImpl::create(const char* characters, unsigned length) +{ + if (!characters || !length) + return empty(); + return adoptRef(new StringImpl(characters, length)); +} + +PassRefPtr<StringImpl> StringImpl::create(const char* string) +{ + if (!string) + return empty(); + unsigned length = strlen(string); + if (!length) + return empty(); + return adoptRef(new StringImpl(string, length)); +} + +PassRefPtr<StringImpl> StringImpl::createWithTerminatingNullCharacter(const StringImpl& string) +{ + return adoptRef(new StringImpl(string, WithTerminatingNullCharacter())); +} + +PassRefPtr<StringImpl> StringImpl::copy() +{ + return adoptRef(new StringImpl(m_data, m_length)); +} + +} // namespace WebCore diff --git a/webkit/pending/StringImpl.h b/webkit/pending/StringImpl.h new file mode 100644 index 0000000..bf3ceca --- /dev/null +++ b/webkit/pending/StringImpl.h @@ -0,0 +1,282 @@ +/* + * Copyright (C) 1999 Lars Knoll (knoll@kde.org) + * Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * + */ + +#ifndef StringImpl_h +#define StringImpl_h + +#include <limits.h> +#include <wtf/ASCIICType.h> +#include <wtf/Forward.h> +#include <wtf/RefCounted.h> +#include <wtf/Vector.h> +#include <wtf/unicode/Unicode.h> + +#if PLATFORM(CF) +typedef const struct __CFString * CFStringRef; +#endif + +#ifdef __OBJC__ +@class NSString; +#endif + +namespace WebCore { + +class AtomicString; +class StringBuffer; + +struct CStringTranslator; +struct HashAndCharactersTranslator; +struct Length; +struct StringHash; +struct UCharBufferTranslator; + +class StringImpl : public RefCounted<StringImpl> { + friend class AtomicString; + friend struct CStringTranslator; + friend struct HashAndCharactersTranslator; + friend struct UCharBufferTranslator; +private: + StringImpl(); + StringImpl(const UChar*, unsigned length); + StringImpl(const char*, unsigned length); + + struct AdoptBuffer { }; + StringImpl(UChar*, unsigned length, AdoptBuffer); + + struct WithTerminatingNullCharacter { }; + StringImpl(const StringImpl&, WithTerminatingNullCharacter); + + // For AtomicString. + StringImpl(const UChar*, unsigned length, unsigned hash); + StringImpl(const char*, unsigned length, unsigned hash); + +public: + ~StringImpl(); + + static PassRefPtr<StringImpl> create(const UChar*, unsigned length); + static PassRefPtr<StringImpl> create(const char*, unsigned length); + static PassRefPtr<StringImpl> create(const char*); + + static PassRefPtr<StringImpl> createWithTerminatingNullCharacter(const StringImpl&); + + static PassRefPtr<StringImpl> createStrippingNullCharacters(const UChar*, unsigned length); + static PassRefPtr<StringImpl> adopt(StringBuffer&); + static PassRefPtr<StringImpl> adopt(Vector<UChar>&); + + const UChar* characters() { return m_data; } + unsigned length() { return m_length; } + + bool hasTerminatingNullCharacter() { return m_hasTerminatingNullCharacter; } + + unsigned hash() { if (m_hash == 0) m_hash = computeHash(m_data, m_length); return m_hash; } + static unsigned computeHash(const UChar*, unsigned len); + static unsigned computeHash(const char*); + + // Makes a deep copy. Helpful only if you need to use a String on another thread. + // Since StringImpl objects are immutable, there's no other reason to make a copy. + PassRefPtr<StringImpl> copy(); + + PassRefPtr<StringImpl> substring(unsigned pos, unsigned len = UINT_MAX); + + UChar operator[](unsigned i) { ASSERT(i < m_length); return m_data[i]; } + UChar32 characterStartingAt(unsigned); + + Length toLength(); + + bool containsOnlyWhitespace(); + + int toIntStrict(bool* ok = 0, int base = 10); + unsigned toUIntStrict(bool* ok = 0, int base = 10); + int64_t toInt64Strict(bool* ok = 0, int base = 10); + uint64_t toUInt64Strict(bool* ok = 0, int base = 10); + + int toInt(bool* ok = 0); // ignores trailing garbage + unsigned toUInt(bool* ok = 0); // ignores trailing garbage + int64_t toInt64(bool* ok = 0); // ignores trailing garbage + uint64_t toUInt64(bool* ok = 0); // ignores trailing garbage + + double toDouble(bool* ok = 0); + float toFloat(bool* ok = 0); + + Length* toCoordsArray(int& len); + Length* toLengthArray(int& len); + bool isLower(); + PassRefPtr<StringImpl> lower(); + PassRefPtr<StringImpl> upper(); + PassRefPtr<StringImpl> secure(UChar aChar); + PassRefPtr<StringImpl> capitalize(UChar previousCharacter); + PassRefPtr<StringImpl> foldCase(); + + PassRefPtr<StringImpl> stripWhiteSpace(); + PassRefPtr<StringImpl> simplifyWhiteSpace(); + + int find(const char*, int index = 0, bool caseSensitive = true); + int find(UChar, int index = 0); + int find(StringImpl*, int index, bool caseSensitive = true); + + int reverseFind(UChar, int index); + int reverseFind(StringImpl*, int index, bool caseSensitive = true); + + bool startsWith(StringImpl* m_data, bool caseSensitive = true) { return find(m_data, 0, caseSensitive) == 0; } + bool endsWith(StringImpl*, bool caseSensitive = true); + + PassRefPtr<StringImpl> replace(UChar, UChar); + PassRefPtr<StringImpl> replace(UChar, StringImpl*); + PassRefPtr<StringImpl> replace(StringImpl*, StringImpl*); + PassRefPtr<StringImpl> replace(unsigned index, unsigned len, StringImpl*); + + static StringImpl* empty(); + + Vector<char> ascii(); + + WTF::Unicode::Direction defaultWritingDirection(); + +#if PLATFORM(CF) + CFStringRef createCFString(); +#endif +#ifdef __OBJC__ + operator NSString*(); +#endif + +private: + unsigned m_length; + const UChar* m_data; + mutable unsigned m_hash; + bool m_inTable; + bool m_hasTerminatingNullCharacter; +}; + +bool equal(StringImpl*, StringImpl*); +bool equal(StringImpl*, const char*); +inline bool equal(const char* a, StringImpl* b) { return equal(b, a); } + +bool equalIgnoringCase(StringImpl*, StringImpl*); +bool equalIgnoringCase(StringImpl*, const char*); +inline bool equalIgnoringCase(const char* a, StringImpl* b) { return equalIgnoringCase(b, a); } + +// Golden ratio - arbitrary start value to avoid mapping all 0's to all 0's +// or anything like that. +const unsigned phi = 0x9e3779b9U; + +// Paul Hsieh's SuperFastHash +// http://www.azillionmonkeys.com/qed/hash.html +inline unsigned StringImpl::computeHash(const UChar* data, unsigned length) +{ + unsigned hash = phi; + + // Main loop. + for (unsigned pairCount = length >> 1; pairCount; pairCount--) { + hash += data[0]; + unsigned tmp = (data[1] << 11) ^ hash; + hash = (hash << 16) ^ tmp; + data += 2; + hash += hash >> 11; + } + + // Handle end case. + if (length & 1) { + hash += data[0]; + hash ^= hash << 11; + hash += hash >> 17; + } + + // Force "avalanching" of final 127 bits. + hash ^= hash << 3; + hash += hash >> 5; + hash ^= hash << 2; + hash += hash >> 15; + hash ^= hash << 10; + + // This avoids ever returning a hash code of 0, since that is used to + // signal "hash not computed yet", using a value that is likely to be + // effectively the same as 0 when the low bits are masked. + hash |= !hash << 31; + + return hash; +} + +// Paul Hsieh's SuperFastHash +// http://www.azillionmonkeys.com/qed/hash.html +inline unsigned StringImpl::computeHash(const char* data) +{ + // This hash is designed to work on 16-bit chunks at a time. But since the normal case + // (above) is to hash UTF-16 characters, we just treat the 8-bit chars as if they + // were 16-bit chunks, which should give matching results + + unsigned hash = phi; + + // Main loop + for (;;) { + unsigned char b0 = data[0]; + if (!b0) + break; + unsigned char b1 = data[1]; + if (!b1) { + hash += b0; + hash ^= hash << 11; + hash += hash >> 17; + break; + } + hash += b0; + unsigned tmp = (b1 << 11) ^ hash; + hash = (hash << 16) ^ tmp; + data += 2; + hash += hash >> 11; + } + + // Force "avalanching" of final 127 bits. + hash ^= hash << 3; + hash += hash >> 5; + hash ^= hash << 2; + hash += hash >> 15; + hash ^= hash << 10; + + // This avoids ever returning a hash code of 0, since that is used to + // signal "hash not computed yet", using a value that is likely to be + // effectively the same as 0 when the low bits are masked. + hash |= !hash << 31; + + return hash; +} + +static inline bool isSpaceOrNewline(UChar c) +{ + // Use isASCIISpace() for basic Latin-1. + // This will include newlines, which aren't included in Unicode DirWS. + return c <= 0x7F ? WTF::isASCIISpace(c) : WTF::Unicode::direction(c) == WTF::Unicode::WhiteSpaceNeutral; +} + +} + +namespace WTF { + + // WebCore::StringHash is the default hash for StringImpl* and RefPtr<StringImpl> + template<typename T> struct DefaultHash; + template<> struct DefaultHash<WebCore::StringImpl*> { + typedef WebCore::StringHash Hash; + }; + template<> struct DefaultHash<RefPtr<WebCore::StringImpl> > { + typedef WebCore::StringHash Hash; + }; + +} + +#endif |