stdlib: Minor refactorings for strings

Remove unused include <errno.h> in KString.cpp.
Rename variables in StringBuilder.reverse().
Use const char* instead of char* in checkParsingErrors.
This commit is contained in:
Ilya Matveev
2017-04-26 15:22:49 +07:00
committed by ilmat192
parent 9aa0777efb
commit 29ce88a6f0
2 changed files with 37 additions and 38 deletions
+2 -3
View File
@@ -14,7 +14,6 @@
* limitations under the License.
*/
#include <errno.h>
#include <limits.h>
#include <stdint.h>
#include <stdlib.h>
@@ -691,13 +690,13 @@ int iswlower_Konan(KChar ch) {
return getType(ch) == LOWERCASE_LETTER;
}
void checkParsingErrors(const char* c_str, char* end, std::string::size_type c_str_size) {
void checkParsingErrors(const char* c_str, const char* end, std::string::size_type c_str_size) {
if (end == c_str) {
ThrowNumberFormatException();
}
// According to http://docs.oracle.com/javase/8/docs/api/java/lang/Double.html#valueOf-java.lang.String-
// trailing whitespace characters must be ignored so we need to do an additional check.
for (char* p = end; p < c_str + c_str_size; p++) {
for (const char* p = end; p < c_str + c_str_size; p++) {
if (!iswspace_Konan(*p)) {
ThrowNumberFormatException();
}
@@ -109,62 +109,62 @@ class StringBuilder private constructor (
}
var end = length - 1
var front = 0
var frontHigh = array[0]
var endLow = array[end]
var allowFrontSur = true
var allowEndSur = true
var frontLeadingChar = array[0]
var endTrailingChar = array[end]
var allowFrontSurrogate = true
var allowEndSurrogate = true
while (front < length / 2) {
var frontLow = array[front + 1]
var endHigh = array[end - 1]
var surAtFront = allowFrontSur && frontLow.isLowSurrogate() && frontHigh.isHighSurrogate()
if (surAtFront && length < 3) {
var frontTrailingChar = array[front + 1]
var endLeadingChar = array[end - 1]
var surrogateAtFront = allowFrontSurrogate && frontTrailingChar.isLowSurrogate() && frontLeadingChar.isHighSurrogate()
if (surrogateAtFront && length < 3) {
return this
}
var surAtEnd = allowEndSur && endLow.isLowSurrogate() && endHigh.isHighSurrogate()
allowFrontSur = true
allowEndSur = true
var surrogateAtEnd = allowEndSurrogate && endTrailingChar.isLowSurrogate() && endLeadingChar.isHighSurrogate()
allowFrontSurrogate = true
allowEndSurrogate = true
when {
surAtFront && surAtEnd -> {
surrogateAtFront && surrogateAtEnd -> {
// Both surrogates - just exchange them.
array[end] = frontLow
array[end - 1] = frontHigh
array[front] = endHigh
array[front + 1] = endLow
frontHigh = array[front + 2]
endLow = array[end - 2]
array[end] = frontTrailingChar
array[end - 1] = frontLeadingChar
array[front] = endLeadingChar
array[front + 1] = endTrailingChar
frontLeadingChar = array[front + 2]
endTrailingChar = array[end - 2]
front++
end--
}
!surAtFront && !surAtEnd -> {
!surrogateAtFront && !surrogateAtEnd -> {
// Neither surrogates - exchange only front/end.
array[end] = frontHigh
array[front] = endLow
frontHigh = frontLow
endLow = endHigh
array[end] = frontLeadingChar
array[front] = endTrailingChar
frontLeadingChar = frontTrailingChar
endTrailingChar = endLeadingChar
}
surAtFront && !surAtEnd -> {
surrogateAtFront && !surrogateAtEnd -> {
// Surrogate only at the front -
// move the low part, the high part will be moved as a usual character on the next iteration.
array[end] = frontLow
array[front] = endLow
endLow = endHigh
allowFrontSur = false
array[end] = frontTrailingChar
array[front] = endTrailingChar
endTrailingChar = endLeadingChar
allowFrontSurrogate = false
}
!surAtFront && surAtEnd -> {
!surrogateAtFront && surrogateAtEnd -> {
// Surrogate only at the end -
// move the high part, the low part will be moved as a usual character on the next iteration.
array[end] = frontHigh
array[front] = endHigh
frontHigh = frontLow
allowEndSur = false
array[end] = frontLeadingChar
array[front] = endLeadingChar
frontLeadingChar = frontTrailingChar
allowEndSurrogate = false
}
}
front++
end--
}
if (length % 2 == 1 && (!allowEndSur || !allowFrontSur)) {
array[end] = if (allowFrontSur) endLow else frontHigh
if (length % 2 == 1 && (!allowEndSurrogate || !allowFrontSurrogate)) {
array[end] = if (allowFrontSurrogate) endTrailingChar else frontLeadingChar
}
return this
}