Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(158)

Side by Side Diff: src/utils.h

Issue 864273005: Scanner / Unicode decoding: use size_t instead of unsigned. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: tentative Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/unicode-inl.h ('k') | test/cctest/test-parsing.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project 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 #ifndef V8_UTILS_H_ 5 #ifndef V8_UTILS_H_
6 #define V8_UTILS_H_ 6 #define V8_UTILS_H_
7 7
8 #include <limits.h> 8 #include <limits.h>
9 #include <stdlib.h> 9 #include <stdlib.h>
10 #include <string.h> 10 #include <string.h>
(...skipping 715 matching lines...) Expand 10 before | Expand all | Expand 10 after
726 } 726 }
727 this->current_chunk_ = new_chunk; 727 this->current_chunk_ = new_chunk;
728 this->index_ = sequence_length; 728 this->index_ = sequence_length;
729 sequence_start_ = 0; 729 sequence_start_ = 0;
730 } 730 }
731 }; 731 };
732 732
733 733
734 // Compare 8bit/16bit chars to 8bit/16bit chars. 734 // Compare 8bit/16bit chars to 8bit/16bit chars.
735 template <typename lchar, typename rchar> 735 template <typename lchar, typename rchar>
736 inline int CompareCharsUnsigned(const lchar* lhs, 736 inline int CompareCharsUnsigned(const lchar* lhs, const rchar* rhs,
737 const rchar* rhs, 737 size_t chars) {
738 int chars) {
739 const lchar* limit = lhs + chars; 738 const lchar* limit = lhs + chars;
740 if (sizeof(*lhs) == sizeof(char) && sizeof(*rhs) == sizeof(char)) { 739 if (sizeof(*lhs) == sizeof(char) && sizeof(*rhs) == sizeof(char)) {
741 // memcmp compares byte-by-byte, yielding wrong results for two-byte 740 // memcmp compares byte-by-byte, yielding wrong results for two-byte
742 // strings on little-endian systems. 741 // strings on little-endian systems.
743 return memcmp(lhs, rhs, chars); 742 return memcmp(lhs, rhs, chars);
744 } 743 }
745 while (lhs < limit) { 744 while (lhs < limit) {
746 int r = static_cast<int>(*lhs) - static_cast<int>(*rhs); 745 int r = static_cast<int>(*lhs) - static_cast<int>(*rhs);
747 if (r != 0) return r; 746 if (r != 0) return r;
748 ++lhs; 747 ++lhs;
749 ++rhs; 748 ++rhs;
750 } 749 }
751 return 0; 750 return 0;
752 } 751 }
753 752
754 template<typename lchar, typename rchar> 753 template <typename lchar, typename rchar>
755 inline int CompareChars(const lchar* lhs, const rchar* rhs, int chars) { 754 inline int CompareChars(const lchar* lhs, const rchar* rhs, size_t chars) {
756 DCHECK(sizeof(lchar) <= 2); 755 DCHECK(sizeof(lchar) <= 2);
757 DCHECK(sizeof(rchar) <= 2); 756 DCHECK(sizeof(rchar) <= 2);
758 if (sizeof(lchar) == 1) { 757 if (sizeof(lchar) == 1) {
759 if (sizeof(rchar) == 1) { 758 if (sizeof(rchar) == 1) {
760 return CompareCharsUnsigned(reinterpret_cast<const uint8_t*>(lhs), 759 return CompareCharsUnsigned(reinterpret_cast<const uint8_t*>(lhs),
761 reinterpret_cast<const uint8_t*>(rhs), 760 reinterpret_cast<const uint8_t*>(rhs),
762 chars); 761 chars);
763 } else { 762 } else {
764 return CompareCharsUnsigned(reinterpret_cast<const uint8_t*>(lhs), 763 return CompareCharsUnsigned(reinterpret_cast<const uint8_t*>(lhs),
765 reinterpret_cast<const uint16_t*>(rhs), 764 reinterpret_cast<const uint16_t*>(rhs),
(...skipping 544 matching lines...) Expand 10 before | Expand all | Expand 10 after
1310 // On return, *exits tells whether the file existed. 1309 // On return, *exits tells whether the file existed.
1311 Vector<const char> ReadFile(const char* filename, 1310 Vector<const char> ReadFile(const char* filename,
1312 bool* exists, 1311 bool* exists,
1313 bool verbose = true); 1312 bool verbose = true);
1314 Vector<const char> ReadFile(FILE* file, 1313 Vector<const char> ReadFile(FILE* file,
1315 bool* exists, 1314 bool* exists,
1316 bool verbose = true); 1315 bool verbose = true);
1317 1316
1318 1317
1319 template <typename sourcechar, typename sinkchar> 1318 template <typename sourcechar, typename sinkchar>
1320 INLINE(static void CopyCharsUnsigned(sinkchar* dest, 1319 INLINE(static void CopyCharsUnsigned(sinkchar* dest, const sourcechar* src,
1321 const sourcechar* src, 1320 size_t chars));
1322 int chars));
1323 #if defined(V8_HOST_ARCH_ARM) 1321 #if defined(V8_HOST_ARCH_ARM)
1324 INLINE(void CopyCharsUnsigned(uint8_t* dest, const uint8_t* src, int chars)); 1322 INLINE(void CopyCharsUnsigned(uint8_t* dest, const uint8_t* src, size_t chars));
1325 INLINE(void CopyCharsUnsigned(uint16_t* dest, const uint8_t* src, int chars)); 1323 INLINE(void CopyCharsUnsigned(uint16_t* dest, const uint8_t* src,
1326 INLINE(void CopyCharsUnsigned(uint16_t* dest, const uint16_t* src, int chars)); 1324 size_t chars));
1325 INLINE(void CopyCharsUnsigned(uint16_t* dest, const uint16_t* src,
1326 size_t chars));
1327 #elif defined(V8_HOST_ARCH_MIPS) 1327 #elif defined(V8_HOST_ARCH_MIPS)
1328 INLINE(void CopyCharsUnsigned(uint8_t* dest, const uint8_t* src, int chars)); 1328 INLINE(void CopyCharsUnsigned(uint8_t* dest, const uint8_t* src, size_t chars));
1329 INLINE(void CopyCharsUnsigned(uint16_t* dest, const uint16_t* src, int chars)); 1329 INLINE(void CopyCharsUnsigned(uint16_t* dest, const uint16_t* src,
1330 size_t chars));
1330 #elif defined(V8_HOST_ARCH_PPC) 1331 #elif defined(V8_HOST_ARCH_PPC)
1331 INLINE(void CopyCharsUnsigned(uint8_t* dest, const uint8_t* src, int chars)); 1332 INLINE(void CopyCharsUnsigned(uint8_t* dest, const uint8_t* src, size_t chars));
1332 INLINE(void CopyCharsUnsigned(uint16_t* dest, const uint16_t* src, int chars)); 1333 INLINE(void CopyCharsUnsigned(uint16_t* dest, const uint16_t* src,
1334 size_t chars));
1333 #endif 1335 #endif
1334 1336
1335 // Copy from 8bit/16bit chars to 8bit/16bit chars. 1337 // Copy from 8bit/16bit chars to 8bit/16bit chars.
1336 template <typename sourcechar, typename sinkchar> 1338 template <typename sourcechar, typename sinkchar>
1337 INLINE(void CopyChars(sinkchar* dest, const sourcechar* src, int chars)); 1339 INLINE(void CopyChars(sinkchar* dest, const sourcechar* src, size_t chars));
1338 1340
1339 template<typename sourcechar, typename sinkchar> 1341 template <typename sourcechar, typename sinkchar>
1340 void CopyChars(sinkchar* dest, const sourcechar* src, int chars) { 1342 void CopyChars(sinkchar* dest, const sourcechar* src, size_t chars) {
1341 DCHECK(sizeof(sourcechar) <= 2); 1343 DCHECK(sizeof(sourcechar) <= 2);
1342 DCHECK(sizeof(sinkchar) <= 2); 1344 DCHECK(sizeof(sinkchar) <= 2);
1343 if (sizeof(sinkchar) == 1) { 1345 if (sizeof(sinkchar) == 1) {
1344 if (sizeof(sourcechar) == 1) { 1346 if (sizeof(sourcechar) == 1) {
1345 CopyCharsUnsigned(reinterpret_cast<uint8_t*>(dest), 1347 CopyCharsUnsigned(reinterpret_cast<uint8_t*>(dest),
1346 reinterpret_cast<const uint8_t*>(src), 1348 reinterpret_cast<const uint8_t*>(src),
1347 chars); 1349 chars);
1348 } else { 1350 } else {
1349 CopyCharsUnsigned(reinterpret_cast<uint8_t*>(dest), 1351 CopyCharsUnsigned(reinterpret_cast<uint8_t*>(dest),
1350 reinterpret_cast<const uint16_t*>(src), 1352 reinterpret_cast<const uint16_t*>(src),
1351 chars); 1353 chars);
1352 } 1354 }
1353 } else { 1355 } else {
1354 if (sizeof(sourcechar) == 1) { 1356 if (sizeof(sourcechar) == 1) {
1355 CopyCharsUnsigned(reinterpret_cast<uint16_t*>(dest), 1357 CopyCharsUnsigned(reinterpret_cast<uint16_t*>(dest),
1356 reinterpret_cast<const uint8_t*>(src), 1358 reinterpret_cast<const uint8_t*>(src),
1357 chars); 1359 chars);
1358 } else { 1360 } else {
1359 CopyCharsUnsigned(reinterpret_cast<uint16_t*>(dest), 1361 CopyCharsUnsigned(reinterpret_cast<uint16_t*>(dest),
1360 reinterpret_cast<const uint16_t*>(src), 1362 reinterpret_cast<const uint16_t*>(src),
1361 chars); 1363 chars);
1362 } 1364 }
1363 } 1365 }
1364 } 1366 }
1365 1367
1366 template <typename sourcechar, typename sinkchar> 1368 template <typename sourcechar, typename sinkchar>
1367 void CopyCharsUnsigned(sinkchar* dest, const sourcechar* src, int chars) { 1369 void CopyCharsUnsigned(sinkchar* dest, const sourcechar* src, size_t chars) {
1368 sinkchar* limit = dest + chars; 1370 sinkchar* limit = dest + chars;
1369 if ((sizeof(*dest) == sizeof(*src)) && 1371 if ((sizeof(*dest) == sizeof(*src)) &&
1370 (chars >= static_cast<int>(kMinComplexMemCopy / sizeof(*dest)))) { 1372 (chars >= static_cast<int>(kMinComplexMemCopy / sizeof(*dest)))) {
1371 MemCopy(dest, src, chars * sizeof(*dest)); 1373 MemCopy(dest, src, chars * sizeof(*dest));
1372 } else { 1374 } else {
1373 while (dest < limit) *dest++ = static_cast<sinkchar>(*src++); 1375 while (dest < limit) *dest++ = static_cast<sinkchar>(*src++);
1374 } 1376 }
1375 } 1377 }
1376 1378
1377 1379
1378 #if defined(V8_HOST_ARCH_ARM) 1380 #if defined(V8_HOST_ARCH_ARM)
1379 void CopyCharsUnsigned(uint8_t* dest, const uint8_t* src, int chars) { 1381 void CopyCharsUnsigned(uint8_t* dest, const uint8_t* src, size_t chars) {
1380 switch (static_cast<unsigned>(chars)) { 1382 switch (static_cast<unsigned>(chars)) {
1381 case 0: 1383 case 0:
1382 break; 1384 break;
1383 case 1: 1385 case 1:
1384 *dest = *src; 1386 *dest = *src;
1385 break; 1387 break;
1386 case 2: 1388 case 2:
1387 memcpy(dest, src, 2); 1389 memcpy(dest, src, 2);
1388 break; 1390 break;
1389 case 3: 1391 case 3:
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
1425 case 15: 1427 case 15:
1426 memcpy(dest, src, 15); 1428 memcpy(dest, src, 15);
1427 break; 1429 break;
1428 default: 1430 default:
1429 MemCopy(dest, src, chars); 1431 MemCopy(dest, src, chars);
1430 break; 1432 break;
1431 } 1433 }
1432 } 1434 }
1433 1435
1434 1436
1435 void CopyCharsUnsigned(uint16_t* dest, const uint8_t* src, int chars) { 1437 void CopyCharsUnsigned(uint16_t* dest, const uint8_t* src, size_t chars) {
1436 if (chars >= kMinComplexConvertMemCopy) { 1438 if (chars >= kMinComplexConvertMemCopy) {
1437 MemCopyUint16Uint8(dest, src, chars); 1439 MemCopyUint16Uint8(dest, src, chars);
1438 } else { 1440 } else {
1439 MemCopyUint16Uint8Wrapper(dest, src, chars); 1441 MemCopyUint16Uint8Wrapper(dest, src, chars);
1440 } 1442 }
1441 } 1443 }
1442 1444
1443 1445
1444 void CopyCharsUnsigned(uint16_t* dest, const uint16_t* src, int chars) { 1446 void CopyCharsUnsigned(uint16_t* dest, const uint16_t* src, size_t chars) {
1445 switch (static_cast<unsigned>(chars)) { 1447 switch (static_cast<unsigned>(chars)) {
1446 case 0: 1448 case 0:
1447 break; 1449 break;
1448 case 1: 1450 case 1:
1449 *dest = *src; 1451 *dest = *src;
1450 break; 1452 break;
1451 case 2: 1453 case 2:
1452 memcpy(dest, src, 4); 1454 memcpy(dest, src, 4);
1453 break; 1455 break;
1454 case 3: 1456 case 3:
(...skipping 12 matching lines...) Expand all
1467 memcpy(dest, src, 14); 1469 memcpy(dest, src, 14);
1468 break; 1470 break;
1469 default: 1471 default:
1470 MemCopy(dest, src, chars * sizeof(*dest)); 1472 MemCopy(dest, src, chars * sizeof(*dest));
1471 break; 1473 break;
1472 } 1474 }
1473 } 1475 }
1474 1476
1475 1477
1476 #elif defined(V8_HOST_ARCH_MIPS) 1478 #elif defined(V8_HOST_ARCH_MIPS)
1477 void CopyCharsUnsigned(uint8_t* dest, const uint8_t* src, int chars) { 1479 void CopyCharsUnsigned(uint8_t* dest, const uint8_t* src, size_t chars) {
1478 if (chars < kMinComplexMemCopy) { 1480 if (chars < kMinComplexMemCopy) {
1479 memcpy(dest, src, chars); 1481 memcpy(dest, src, chars);
1480 } else { 1482 } else {
1481 MemCopy(dest, src, chars); 1483 MemCopy(dest, src, chars);
1482 } 1484 }
1483 } 1485 }
1484 1486
1485 void CopyCharsUnsigned(uint16_t* dest, const uint16_t* src, int chars) { 1487 void CopyCharsUnsigned(uint16_t* dest, const uint16_t* src, size_t chars) {
1486 if (chars < kMinComplexMemCopy) { 1488 if (chars < kMinComplexMemCopy) {
1487 memcpy(dest, src, chars * sizeof(*dest)); 1489 memcpy(dest, src, chars * sizeof(*dest));
1488 } else { 1490 } else {
1489 MemCopy(dest, src, chars * sizeof(*dest)); 1491 MemCopy(dest, src, chars * sizeof(*dest));
1490 } 1492 }
1491 } 1493 }
1492 #elif defined(V8_HOST_ARCH_PPC) 1494 #elif defined(V8_HOST_ARCH_PPC)
1493 #define CASE(n) \ 1495 #define CASE(n) \
1494 case n: \ 1496 case n: \
1495 memcpy(dest, src, n); \ 1497 memcpy(dest, src, n); \
1496 break 1498 break
1497 void CopyCharsUnsigned(uint8_t* dest, const uint8_t* src, int chars) { 1499 void CopyCharsUnsigned(uint8_t* dest, const uint8_t* src, size_t chars) {
1498 switch (static_cast<unsigned>(chars)) { 1500 switch (static_cast<unsigned>(chars)) {
1499 case 0: 1501 case 0:
1500 break; 1502 break;
1501 case 1: 1503 case 1:
1502 *dest = *src; 1504 *dest = *src;
1503 break; 1505 break;
1504 CASE(2); 1506 CASE(2);
1505 CASE(3); 1507 CASE(3);
1506 CASE(4); 1508 CASE(4);
1507 CASE(5); 1509 CASE(5);
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
1568 memcpy(dest, src, chars); 1570 memcpy(dest, src, chars);
1569 break; 1571 break;
1570 } 1572 }
1571 } 1573 }
1572 #undef CASE 1574 #undef CASE
1573 1575
1574 #define CASE(n) \ 1576 #define CASE(n) \
1575 case n: \ 1577 case n: \
1576 memcpy(dest, src, n * 2); \ 1578 memcpy(dest, src, n * 2); \
1577 break 1579 break
1578 void CopyCharsUnsigned(uint16_t* dest, const uint16_t* src, int chars) { 1580 void CopyCharsUnsigned(uint16_t* dest, const uint16_t* src, size_t chars) {
1579 switch (static_cast<unsigned>(chars)) { 1581 switch (static_cast<unsigned>(chars)) {
1580 case 0: 1582 case 0:
1581 break; 1583 break;
1582 case 1: 1584 case 1:
1583 *dest = *src; 1585 *dest = *src;
1584 break; 1586 break;
1585 CASE(2); 1587 CASE(2);
1586 CASE(3); 1588 CASE(3);
1587 CASE(4); 1589 CASE(4);
1588 CASE(5); 1590 CASE(5);
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
1673 // Takes the address of the limit variable in order to find out where 1675 // Takes the address of the limit variable in order to find out where
1674 // the top of stack is right now. 1676 // the top of stack is right now.
1675 uintptr_t limit = reinterpret_cast<uintptr_t>(&limit); 1677 uintptr_t limit = reinterpret_cast<uintptr_t>(&limit);
1676 return limit; 1678 return limit;
1677 } 1679 }
1678 1680
1679 } // namespace internal 1681 } // namespace internal
1680 } // namespace v8 1682 } // namespace v8
1681 1683
1682 #endif // V8_UTILS_H_ 1684 #endif // V8_UTILS_H_
OLDNEW
« no previous file with comments | « src/unicode-inl.h ('k') | test/cctest/test-parsing.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698