| OLD | NEW |
| 1 /* | 1 /* |
| 2 * (C) 1999 Lars Knoll (knoll@kde.org) | 2 * (C) 1999 Lars Knoll (knoll@kde.org) |
| 3 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2010, 2012 Apple Inc. All rights | 3 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2010, 2012 Apple Inc. All rights |
| 4 * reserved. | 4 * reserved. |
| 5 * Copyright (C) 2007-2009 Torch Mobile, Inc. | 5 * Copyright (C) 2007-2009 Torch Mobile, Inc. |
| 6 * | 6 * |
| 7 * This library is free software; you can redistribute it and/or | 7 * This library is free software; you can redistribute it and/or |
| 8 * modify it under the terms of the GNU Library General Public | 8 * modify it under the terms of the GNU Library General Public |
| 9 * License as published by the Free Software Foundation; either | 9 * License as published by the Free Software Foundation; either |
| 10 * version 2 of the License, or (at your option) any later version. | 10 * version 2 of the License, or (at your option) any later version. |
| (...skipping 517 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 528 result.append(substring(startPos)); | 528 result.append(substring(startPos)); |
| 529 } | 529 } |
| 530 | 530 |
| 531 CString String::ascii() const { | 531 CString String::ascii() const { |
| 532 // Printable ASCII characters 32..127 and the null character are | 532 // Printable ASCII characters 32..127 and the null character are |
| 533 // preserved, characters outside of this range are converted to '?'. | 533 // preserved, characters outside of this range are converted to '?'. |
| 534 | 534 |
| 535 unsigned length = this->length(); | 535 unsigned length = this->length(); |
| 536 if (!length) { | 536 if (!length) { |
| 537 char* characterBuffer; | 537 char* characterBuffer; |
| 538 return CString::newUninitialized(length, characterBuffer); | 538 return CString::createUninitialized(length, characterBuffer); |
| 539 } | 539 } |
| 540 | 540 |
| 541 if (this->is8Bit()) { | 541 if (this->is8Bit()) { |
| 542 const LChar* characters = this->characters8(); | 542 const LChar* characters = this->characters8(); |
| 543 | 543 |
| 544 char* characterBuffer; | 544 char* characterBuffer; |
| 545 CString result = CString::newUninitialized(length, characterBuffer); | 545 CString result = CString::createUninitialized(length, characterBuffer); |
| 546 | 546 |
| 547 for (unsigned i = 0; i < length; ++i) { | 547 for (unsigned i = 0; i < length; ++i) { |
| 548 LChar ch = characters[i]; | 548 LChar ch = characters[i]; |
| 549 characterBuffer[i] = ch && (ch < 0x20 || ch > 0x7f) ? '?' : ch; | 549 characterBuffer[i] = ch && (ch < 0x20 || ch > 0x7f) ? '?' : ch; |
| 550 } | 550 } |
| 551 | 551 |
| 552 return result; | 552 return result; |
| 553 } | 553 } |
| 554 | 554 |
| 555 const UChar* characters = this->characters16(); | 555 const UChar* characters = this->characters16(); |
| 556 | 556 |
| 557 char* characterBuffer; | 557 char* characterBuffer; |
| 558 CString result = CString::newUninitialized(length, characterBuffer); | 558 CString result = CString::createUninitialized(length, characterBuffer); |
| 559 | 559 |
| 560 for (unsigned i = 0; i < length; ++i) { | 560 for (unsigned i = 0; i < length; ++i) { |
| 561 UChar ch = characters[i]; | 561 UChar ch = characters[i]; |
| 562 characterBuffer[i] = | 562 characterBuffer[i] = |
| 563 ch && (ch < 0x20 || ch > 0x7f) ? '?' : static_cast<char>(ch); | 563 ch && (ch < 0x20 || ch > 0x7f) ? '?' : static_cast<char>(ch); |
| 564 } | 564 } |
| 565 | 565 |
| 566 return result; | 566 return result; |
| 567 } | 567 } |
| 568 | 568 |
| 569 CString String::latin1() const { | 569 CString String::latin1() const { |
| 570 // Basic Latin1 (ISO) encoding - Unicode characters 0..255 are | 570 // Basic Latin1 (ISO) encoding - Unicode characters 0..255 are |
| 571 // preserved, characters outside of this range are converted to '?'. | 571 // preserved, characters outside of this range are converted to '?'. |
| 572 | 572 |
| 573 unsigned length = this->length(); | 573 unsigned length = this->length(); |
| 574 | 574 |
| 575 if (!length) | 575 if (!length) |
| 576 return CString("", 0); | 576 return CString("", 0); |
| 577 | 577 |
| 578 if (is8Bit()) | 578 if (is8Bit()) |
| 579 return CString(reinterpret_cast<const char*>(this->characters8()), length); | 579 return CString(reinterpret_cast<const char*>(this->characters8()), length); |
| 580 | 580 |
| 581 const UChar* characters = this->characters16(); | 581 const UChar* characters = this->characters16(); |
| 582 | 582 |
| 583 char* characterBuffer; | 583 char* characterBuffer; |
| 584 CString result = CString::newUninitialized(length, characterBuffer); | 584 CString result = CString::createUninitialized(length, characterBuffer); |
| 585 | 585 |
| 586 for (unsigned i = 0; i < length; ++i) { | 586 for (unsigned i = 0; i < length; ++i) { |
| 587 UChar ch = characters[i]; | 587 UChar ch = characters[i]; |
| 588 characterBuffer[i] = ch > 0xff ? '?' : static_cast<char>(ch); | 588 characterBuffer[i] = ch > 0xff ? '?' : static_cast<char>(ch); |
| 589 } | 589 } |
| 590 | 590 |
| 591 return result; | 591 return result; |
| 592 } | 592 } |
| 593 | 593 |
| 594 // Helper to write a three-byte UTF-8 code point to the buffer, caller must | 594 // Helper to write a three-byte UTF-8 code point to the buffer, caller must |
| 595 // check room is available. | 595 // check room is available. |
| 596 static inline void putUTF8Triple(char*& buffer, UChar ch) { | 596 static inline void putUTF8Triple(char*& buffer, UChar ch) { |
| 597 ASSERT(ch >= 0x0800); | 597 ASSERT(ch >= 0x0800); |
| 598 *buffer++ = static_cast<char>(((ch >> 12) & 0x0F) | 0xE0); | 598 *buffer++ = static_cast<char>(((ch >> 12) & 0x0F) | 0xE0); |
| 599 *buffer++ = static_cast<char>(((ch >> 6) & 0x3F) | 0x80); | 599 *buffer++ = static_cast<char>(((ch >> 6) & 0x3F) | 0x80); |
| 600 *buffer++ = static_cast<char>((ch & 0x3F) | 0x80); | 600 *buffer++ = static_cast<char>((ch & 0x3F) | 0x80); |
| 601 } | 601 } |
| 602 | 602 |
| 603 CString String::utf8(UTF8ConversionMode mode) const { | 603 CString String::utf8(UTF8ConversionMode mode) const { |
| 604 unsigned length = this->length(); | 604 unsigned length = this->length(); |
| 605 | 605 |
| 606 if (!length) | 606 if (!length) |
| 607 return CString("", 0); | 607 return CString("", 0); |
| 608 | 608 |
| 609 // Allocate a buffer big enough to hold all the characters | 609 // Allocate a buffer big enough to hold all the characters |
| 610 // (an individual UTF-16 UChar can only expand to 3 UTF-8 bytes). | 610 // (an individual UTF-16 UChar can only expand to 3 UTF-8 bytes). |
| 611 // Optimization ideas, if we find this function is hot: | 611 // Optimization ideas, if we find this function is hot: |
| 612 // * We could speculatively create a CStringBuffer to contain 'length' | 612 // * We could speculatively create a CStringImpl to contain 'length' |
| 613 // characters, and resize if necessary (i.e. if the buffer contains | 613 // characters, and resize if necessary (i.e. if the buffer contains |
| 614 // non-ascii characters). (Alternatively, scan the buffer first for | 614 // non-ascii characters). (Alternatively, scan the buffer first for |
| 615 // ascii characters, so we know this will be sufficient). | 615 // ascii characters, so we know this will be sufficient). |
| 616 // * We could allocate a CStringBuffer with an appropriate size to | 616 // * We could allocate a CStringImpl with an appropriate size to |
| 617 // have a good chance of being able to write the string into the | 617 // have a good chance of being able to write the string into the |
| 618 // buffer without reallocing (say, 1.5 x length). | 618 // buffer without reallocing (say, 1.5 x length). |
| 619 if (length > std::numeric_limits<unsigned>::max() / 3) | 619 if (length > std::numeric_limits<unsigned>::max() / 3) |
| 620 return CString(); | 620 return CString(); |
| 621 Vector<char, 1024> bufferVector(length * 3); | 621 Vector<char, 1024> bufferVector(length * 3); |
| 622 | 622 |
| 623 char* buffer = bufferVector.data(); | 623 char* buffer = bufferVector.data(); |
| 624 | 624 |
| 625 if (is8Bit()) { | 625 if (is8Bit()) { |
| 626 const LChar* characters = this->characters8(); | 626 const LChar* characters = this->characters8(); |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 844 } | 844 } |
| 845 buffer.append('\0'); | 845 buffer.append('\0'); |
| 846 return buffer; | 846 return buffer; |
| 847 } | 847 } |
| 848 | 848 |
| 849 Vector<char> asciiDebug(String& string) { | 849 Vector<char> asciiDebug(String& string) { |
| 850 return asciiDebug(string.impl()); | 850 return asciiDebug(string.impl()); |
| 851 } | 851 } |
| 852 | 852 |
| 853 #endif | 853 #endif |
| OLD | NEW |