~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/3rdparty/webkit/WebCore/platform/text/String.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2009-11-02 18:30:08 UTC
  • mfrom: (1.2.2 upstream)
  • mto: (15.2.5 experimental)
  • mto: This revision was merged to the branch mainline in revision 88.
  • Revision ID: james.westby@ubuntu.com-20091102183008-b6a4gcs128mvfb3m
Tags: upstream-4.6.0~beta1
ImportĀ upstreamĀ versionĀ 4.6.0~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * (C) 1999 Lars Knoll (knoll@kde.org)
3
3
 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
 
4
 * Copyright (C) 2007-2009 Torch Mobile, Inc.
4
5
 *
5
6
 * This library is free software; you can redistribute it and/or
6
7
 * modify it under the terms of the GNU Library General Public
24
25
#include "CString.h"
25
26
#include "FloatConversion.h"
26
27
#include "StringBuffer.h"
 
28
#include "TextBreakIterator.h"
27
29
#include "TextEncoding.h"
28
30
#include <wtf/dtoa.h>
29
31
#include <limits>
85
87
    // call to fastMalloc every single time.
86
88
    if (str.m_impl) {
87
89
        if (m_impl) {
88
 
            StringBuffer buffer(m_impl->length() + str.length());
89
 
            memcpy(buffer.characters(), m_impl->characters(), m_impl->length() * sizeof(UChar));
90
 
            memcpy(buffer.characters() + m_impl->length(), str.characters(), str.length() * sizeof(UChar));
91
 
            m_impl = StringImpl::adopt(buffer);
 
90
            UChar* data;
 
91
            RefPtr<StringImpl> newImpl =
 
92
                StringImpl::createUninitialized(m_impl->length() + str.length(), data);
 
93
            memcpy(data, m_impl->characters(), m_impl->length() * sizeof(UChar));
 
94
            memcpy(data + m_impl->length(), str.characters(), str.length() * sizeof(UChar));
 
95
            m_impl = newImpl.release();
92
96
        } else
93
97
            m_impl = str.m_impl;
94
98
    }
101
105
    // one String is pointing at this StringImpl, but even then it's going to require a
102
106
    // call to fastMalloc every single time.
103
107
    if (m_impl) {
104
 
        StringBuffer buffer(m_impl->length() + 1);
105
 
        memcpy(buffer.characters(), m_impl->characters(), m_impl->length() * sizeof(UChar));
106
 
        buffer[m_impl->length()] = c;
107
 
        m_impl = StringImpl::adopt(buffer);
 
108
        UChar* data;
 
109
        RefPtr<StringImpl> newImpl =
 
110
            StringImpl::createUninitialized(m_impl->length() + 1, data);
 
111
        memcpy(data, m_impl->characters(), m_impl->length() * sizeof(UChar));
 
112
        data[m_impl->length()] = c;
 
113
        m_impl = newImpl.release();
108
114
    } else
109
115
        m_impl = StringImpl::create(&c, 1);
110
116
}
116
122
    // one String is pointing at this StringImpl, but even then it's going to require a
117
123
    // call to fastMalloc every single time.
118
124
    if (m_impl) {
119
 
        StringBuffer buffer(m_impl->length() + 1);
120
 
        memcpy(buffer.characters(), m_impl->characters(), m_impl->length() * sizeof(UChar));
121
 
        buffer[m_impl->length()] = c;
122
 
        m_impl = StringImpl::adopt(buffer);
 
125
        UChar* data;
 
126
        RefPtr<StringImpl> newImpl =
 
127
            StringImpl::createUninitialized(m_impl->length() + 1, data);
 
128
        memcpy(data, m_impl->characters(), m_impl->length() * sizeof(UChar));
 
129
        data[m_impl->length()] = c;
 
130
        m_impl = newImpl.release();
123
131
    } else
124
132
        m_impl = StringImpl::create(&c, 1);
125
133
}
170
178
        return;
171
179
 
172
180
    ASSERT(charactersToAppend);
173
 
    StringBuffer buffer(length() + lengthToAppend);
174
 
    memcpy(buffer.characters(), characters(), length() * sizeof(UChar));
175
 
    memcpy(buffer.characters() + length(), charactersToAppend, lengthToAppend * sizeof(UChar));
176
 
    m_impl = StringImpl::adopt(buffer);
 
181
    UChar* data;
 
182
    RefPtr<StringImpl> newImpl =
 
183
        StringImpl::createUninitialized(length() + lengthToAppend, data);
 
184
    memcpy(data, characters(), length() * sizeof(UChar));
 
185
    memcpy(data + length(), charactersToAppend, lengthToAppend * sizeof(UChar));
 
186
    m_impl = newImpl.release();
177
187
}
178
188
 
179
189
void String::insert(const UChar* charactersToInsert, unsigned lengthToInsert, unsigned position)
189
199
        return;
190
200
 
191
201
    ASSERT(charactersToInsert);
192
 
    StringBuffer buffer(length() + lengthToInsert);
193
 
    memcpy(buffer.characters(), characters(), position * sizeof(UChar));
194
 
    memcpy(buffer.characters() + position, charactersToInsert, lengthToInsert * sizeof(UChar));
195
 
    memcpy(buffer.characters() + position + lengthToInsert, characters() + position, (length() - position) * sizeof(UChar));
196
 
    m_impl = StringImpl::adopt(buffer);
 
202
    UChar* data;
 
203
    RefPtr<StringImpl> newImpl =
 
204
      StringImpl::createUninitialized(length() + lengthToInsert, data);
 
205
    memcpy(data, characters(), position * sizeof(UChar));
 
206
    memcpy(data + position, charactersToInsert, lengthToInsert * sizeof(UChar));
 
207
    memcpy(data + position + lengthToInsert, characters() + position, (length() - position) * sizeof(UChar));
 
208
    m_impl = newImpl.release();
197
209
}
198
210
 
199
211
UChar String::operator[](unsigned i) const
221
233
{
222
234
    if (position >= length())
223
235
        return;
224
 
    StringBuffer buffer(position);
225
 
    memcpy(buffer.characters(), characters(), position * sizeof(UChar));
226
 
    m_impl = StringImpl::adopt(buffer);
 
236
    UChar* data;
 
237
    RefPtr<StringImpl> newImpl = StringImpl::createUninitialized(position, data);
 
238
    memcpy(data, characters(), position * sizeof(UChar));
 
239
    m_impl = newImpl.release();
227
240
}
228
241
 
229
242
void String::remove(unsigned position, int lengthToRemove)
234
247
        return;
235
248
    if (static_cast<unsigned>(lengthToRemove) > length() - position)
236
249
        lengthToRemove = length() - position;
237
 
    StringBuffer buffer(length() - lengthToRemove);
238
 
    memcpy(buffer.characters(), characters(), position * sizeof(UChar));
239
 
    memcpy(buffer.characters() + position, characters() + position + lengthToRemove,
 
250
    UChar* data;
 
251
    RefPtr<StringImpl> newImpl =
 
252
        StringImpl::createUninitialized(length() - lengthToRemove, data);
 
253
    memcpy(data, characters(), position * sizeof(UChar));
 
254
    memcpy(data + position, characters() + position + lengthToRemove,
240
255
        (length() - lengthToRemove - position) * sizeof(UChar));
241
 
    m_impl = StringImpl::adopt(buffer);
 
256
    m_impl = newImpl.release();
242
257
}
243
258
 
244
259
String String::substring(unsigned pos, unsigned len) const
340
355
    va_end(args);
341
356
 
342
357
    return buffer;
 
358
 
 
359
#elif PLATFORM(WINCE)
 
360
    va_list args;
 
361
    va_start(args, format);
 
362
 
 
363
    Vector<char, 256> buffer;
 
364
 
 
365
    int bufferSize = 256;
 
366
    buffer.resize(bufferSize);
 
367
    for (;;) {
 
368
        int written = vsnprintf(buffer.data(), bufferSize, format, args);
 
369
        va_end(args);
 
370
 
 
371
        if (written == 0)
 
372
            return String("");
 
373
        if (written > 0)
 
374
            return StringImpl::create(buffer.data(), written);
 
375
        
 
376
        bufferSize <<= 1;
 
377
        buffer.resize(bufferSize);
 
378
        va_start(args, format);
 
379
    }
 
380
 
343
381
#else
344
382
    va_list args;
345
383
    va_start(args, format);
378
416
#endif
379
417
}
380
418
 
 
419
String String::number(short n)
 
420
{
 
421
    return String::format("%hd", n);
 
422
}
 
423
 
 
424
String String::number(unsigned short n)
 
425
{
 
426
    return String::format("%hu", n);
 
427
}
 
428
 
381
429
String String::number(int n)
382
430
{
383
431
    return String::format("%d", n);
461
509
    return m_impl->toUInt64Strict(ok, base);
462
510
}
463
511
 
 
512
intptr_t String::toIntPtrStrict(bool* ok, int base) const
 
513
{
 
514
    if (!m_impl) {
 
515
        if (ok)
 
516
            *ok = false;
 
517
        return 0;
 
518
    }
 
519
    return m_impl->toIntPtrStrict(ok, base);
 
520
}
 
521
 
 
522
 
464
523
int String::toInt(bool* ok) const
465
524
{
466
525
    if (!m_impl) {
501
560
    return m_impl->toUInt64(ok);
502
561
}
503
562
 
 
563
intptr_t String::toIntPtr(bool* ok) const
 
564
{
 
565
    if (!m_impl) {
 
566
        if (ok)
 
567
            *ok = false;
 
568
        return 0;
 
569
    }
 
570
    return m_impl->toIntPtr(ok);
 
571
}
 
572
 
504
573
double String::toDouble(bool* ok) const
505
574
{
506
575
    if (!m_impl) {
613
682
    return UTF8Encoding().decode(string, strlen(string));
614
683
}
615
684
 
 
685
String String::fromUTF8WithLatin1Fallback(const char* string, size_t size)
 
686
{
 
687
    String result = fromUTF8(string, size);
 
688
    if (!result)
 
689
        result = String(string, size);
 
690
    
 
691
    return result;
 
692
}
 
693
 
616
694
#if USE(JSC)
617
695
String::String(const Identifier& str)
618
696
{
619
697
    if (str.isNull())
620
698
        return;
621
 
    m_impl = StringImpl::create(str.data(), str.size());
 
699
    m_impl = StringImpl::create(str.ustring());
622
700
}
623
701
 
624
702
String::String(const UString& str)
625
703
{
626
704
    if (str.isNull())
627
705
        return;
628
 
    m_impl = StringImpl::create(str.data(), str.size());
 
706
    m_impl = StringImpl::create(str);
629
707
}
630
708
 
631
709
String::operator UString() const
632
710
{
633
711
    if (!m_impl)
634
712
        return UString();
635
 
    return UString(m_impl->characters(), m_impl->length());
 
713
    return m_impl->ustring();
636
714
}
637
715
#endif
638
716
 
772
850
    return toIntegralType<uint64_t>(data, length, ok, base);
773
851
}
774
852
 
 
853
intptr_t charactersToIntPtrStrict(const UChar* data, size_t length, bool* ok, int base)
 
854
{
 
855
    return toIntegralType<intptr_t>(data, length, ok, base);
 
856
}
 
857
 
775
858
int charactersToInt(const UChar* data, size_t length, bool* ok)
776
859
{
777
860
    return toIntegralType<int>(data, lengthOfCharactersAsInteger(data, length), ok, 10);
792
875
    return toIntegralType<uint64_t>(data, lengthOfCharactersAsInteger(data, length), ok, 10);
793
876
}
794
877
 
 
878
intptr_t charactersToIntPtr(const UChar* data, size_t length, bool* ok)
 
879
{
 
880
    return toIntegralType<intptr_t>(data, lengthOfCharactersAsInteger(data, length), ok, 10);
 
881
}
 
882
 
795
883
double charactersToDouble(const UChar* data, size_t length, bool* ok)
796
884
{
797
885
    if (!length) {
834
922
    return SharedBuffer::adoptVector(buffer);
835
923
}
836
924
 
 
925
unsigned String::numGraphemeClusters() const
 
926
{
 
927
    TextBreakIterator* it = characterBreakIterator(characters(), length());
 
928
    if (!it)
 
929
        return length();
 
930
 
 
931
    unsigned num = 0;
 
932
    while (textBreakNext(it) != TextBreakDone)
 
933
        ++num;
 
934
    return num;
 
935
}
 
936
 
 
937
unsigned String::numCharactersInGraphemeClusters(unsigned numGraphemeClusters) const
 
938
{
 
939
    TextBreakIterator* it = characterBreakIterator(characters(), length());
 
940
    if (!it)
 
941
        return min(length(), numGraphemeClusters);
 
942
 
 
943
    for (unsigned i = 0; i < numGraphemeClusters; ++i) {
 
944
        if (textBreakNext(it) == TextBreakDone)
 
945
            return length();
 
946
    }
 
947
    return textBreakCurrent(it);
 
948
}
 
949
 
837
950
} // namespace WebCore
838
951
 
839
952
#ifndef NDEBUG
840
 
// For debugging only -- leaks memory
 
953
// For use in the debugger - leaks memory
 
954
WebCore::String* string(const char*);
 
955
 
841
956
WebCore::String* string(const char* s)
842
957
{
843
958
    return new WebCore::String(s);