| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "platform/inspector_protocol/String16STL.h" | 5 #include "platform/inspector_protocol/String16STL.h" |
| 6 | 6 |
| 7 #include "platform/inspector_protocol/Platform.h" | 7 #include "platform/inspector_protocol/Platform.h" |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <cctype> | 10 #include <cctype> |
| (...skipping 544 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 555 // ascii characters, so we know this will be sufficient). | 555 // ascii characters, so we know this will be sufficient). |
| 556 // * We could allocate a CStringBuffer with an appropriate size to | 556 // * We could allocate a CStringBuffer with an appropriate size to |
| 557 // have a good chance of being able to write the string into the | 557 // have a good chance of being able to write the string into the |
| 558 // buffer without reallocing (say, 1.5 x length). | 558 // buffer without reallocing (say, 1.5 x length). |
| 559 if (length > std::numeric_limits<unsigned>::max() / 3) | 559 if (length > std::numeric_limits<unsigned>::max() / 3) |
| 560 return std::string(); | 560 return std::string(); |
| 561 std::vector<char> bufferVector(length * 3); | 561 std::vector<char> bufferVector(length * 3); |
| 562 char* buffer = bufferVector.data(); | 562 char* buffer = bufferVector.data(); |
| 563 const UChar* characters = m_impl.data(); | 563 const UChar* characters = m_impl.data(); |
| 564 | 564 |
| 565 bool strict = false; | 565 ConversionResult result = convertUTF16ToUTF8(&characters, characters + lengt
h, &buffer, buffer + bufferVector.size(), false); |
| 566 ConversionResult result = convertUTF16ToUTF8(&characters, characters + lengt
h, &buffer, buffer + bufferVector.size(), strict); | |
| 567 DCHECK(result != targetExhausted); // (length * 3) should be sufficient for
any conversion | 566 DCHECK(result != targetExhausted); // (length * 3) should be sufficient for
any conversion |
| 568 | 567 |
| 569 // Only produced from strict conversion. | 568 // Only produced from strict conversion. |
| 570 if (result == sourceIllegal) { | 569 DCHECK(result != sourceIllegal); |
| 571 DCHECK(strict); | |
| 572 return std::string(); | |
| 573 } | |
| 574 | 570 |
| 575 // Check for an unconverted high surrogate. | 571 // Check for an unconverted high surrogate. |
| 576 if (result == sourceExhausted) { | 572 if (result == sourceExhausted) { |
| 577 if (strict) | |
| 578 return std::string(); | |
| 579 // This should be one unpaired high surrogate. Treat it the same | 573 // This should be one unpaired high surrogate. Treat it the same |
| 580 // was as an unpaired high surrogate would have been handled in | 574 // was as an unpaired high surrogate would have been handled in |
| 581 // the middle of a string with non-strict conversion - which is | 575 // the middle of a string with non-strict conversion - which is |
| 582 // to say, simply encode it to UTF-8. | 576 // to say, simply encode it to UTF-8. |
| 583 DCHECK((characters + 1) == (m_impl.data() + length)); | 577 DCHECK((characters + 1) == (m_impl.data() + length)); |
| 584 DCHECK((*characters >= 0xD800) && (*characters <= 0xDBFF)); | 578 DCHECK((*characters >= 0xD800) && (*characters <= 0xDBFF)); |
| 585 // There should be room left, since one UChar hasn't been | 579 // There should be room left, since one UChar hasn't been |
| 586 // converted. | 580 // converted. |
| 587 DCHECK((buffer + 3) <= (buffer + bufferVector.size())); | 581 DCHECK((buffer + 3) <= (buffer + bufferVector.size())); |
| 588 putUTF8Triple(buffer, *characters); | 582 putUTF8Triple(buffer, *characters); |
| 589 } | 583 } |
| 590 | 584 |
| 591 return std::string(bufferVector.data(), buffer - bufferVector.data()); | 585 return std::string(bufferVector.data(), buffer - bufferVector.data()); |
| 592 } | 586 } |
| 593 | 587 |
| 594 String16 String16::stripWhiteSpace() const | 588 String16 String16::stripWhiteSpace() const |
| 595 { | 589 { |
| 596 wstring result(m_impl); | 590 wstring result(m_impl); |
| 597 trim(result); | 591 trim(result); |
| 598 return result; | 592 return result; |
| 599 } | 593 } |
| 600 | 594 |
| 601 } // namespace protocol | 595 } // namespace protocol |
| 602 } // namespace blink | 596 } // namespace blink |
| OLD | NEW |