1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647
|
// Copyright Contributors to the Pystring project.
// SPDX-License-Identifier: BSD-3-Clause
// https://github.com/imageworks/pystring/blob/master/LICENSE
#include "pystring.h"
#include <algorithm>
#include <cctype>
#include <cstring>
#include <iostream>
#include <sstream>
namespace pystring
{
#if defined(_WIN32) || defined(_WIN64) || defined(_WINDOWS) || defined(_MSC_VER)
#ifndef WINDOWS
#define WINDOWS
#endif
#endif
// This definition codes from configure.in in the python src.
// Strictly speaking this limits us to str sizes of 2**31.
// Should we wish to handle this limit, we could use an architecture
// specific #defines and read from ssize_t (unistd.h) if the header exists.
// But in the meantime, the use of int assures maximum arch compatibility.
// This must also equal the size used in the end = MAX_32BIT_INT default arg.
typedef int Py_ssize_t;
const std::string forward_slash = "/";
const std::string double_forward_slash = "//";
const std::string triple_forward_slash = "///";
const std::string double_back_slash = "\\";
const std::string empty_string = "";
const std::string dot = ".";
const std::string double_dot = "..";
const std::string colon = ":";
/* helper macro to fixup start/end slice values */
#define ADJUST_INDICES(start, end, len) \
if (end > len) \
end = len; \
else if (end < 0) { \
end += len; \
if (end < 0) \
end = 0; \
} \
if (start < 0) { \
start += len; \
if (start < 0) \
start = 0; \
}
namespace {
//////////////////////////////////////////////////////////////////////////////////////////////
/// why doesn't the std::reverse work?
///
void reverse_strings( std::vector< std::string > & result)
{
for (std::vector< std::string >::size_type i = 0; i < result.size() / 2; i++ )
{
std::swap(result[i], result[result.size() - 1 - i]);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
void split_whitespace( const std::string & str, std::vector< std::string > & result, int maxsplit )
{
std::string::size_type i, j, len = str.size();
for (i = j = 0; i < len; )
{
while ( i < len && ::isspace( str[i] ) ) i++;
j = i;
while ( i < len && ! ::isspace( str[i]) ) i++;
if (j < i)
{
if ( maxsplit-- <= 0 ) break;
result.push_back( str.substr( j, i - j ));
while ( i < len && ::isspace( str[i])) i++;
j = i;
}
}
if (j < len)
{
result.push_back( str.substr( j, len - j ));
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
void rsplit_whitespace( const std::string & str, std::vector< std::string > & result, int maxsplit )
{
std::string::size_type len = str.size();
std::string::size_type i, j;
for (i = j = len; i > 0; )
{
while ( i > 0 && ::isspace( str[i - 1] ) ) i--;
j = i;
while ( i > 0 && ! ::isspace( str[i - 1]) ) i--;
if (j > i)
{
if ( maxsplit-- <= 0 ) break;
result.push_back( str.substr( i, j - i ));
while ( i > 0 && ::isspace( str[i - 1])) i--;
j = i;
}
}
if (j > 0)
{
result.push_back( str.substr( 0, j ));
}
//std::reverse( result, result.begin(), result.end() );
reverse_strings( result );
}
} //anonymous namespace
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
void split( const std::string & str, std::vector< std::string > & result, const std::string & sep, int maxsplit )
{
result.clear();
if ( maxsplit < 0 ) maxsplit = MAX_32BIT_INT;//result.max_size();
if ( sep.size() == 0 )
{
split_whitespace( str, result, maxsplit );
return;
}
std::string::size_type i,j, len = str.size(), n = sep.size();
i = j = 0;
while ( i+n <= len )
{
if ( str[i] == sep[0] && str.substr( i, n ) == sep )
{
if ( maxsplit-- <= 0 ) break;
result.push_back( str.substr( j, i - j ) );
i = j = i + n;
}
else
{
i++;
}
}
result.push_back( str.substr( j, len-j ) );
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
void rsplit( const std::string & str, std::vector< std::string > & result, const std::string & sep, int maxsplit )
{
if ( maxsplit < 0 )
{
split( str, result, sep, maxsplit );
return;
}
result.clear();
if ( sep.size() == 0 )
{
rsplit_whitespace( str, result, maxsplit );
return;
}
Py_ssize_t i,j, len = (Py_ssize_t) str.size(), n = (Py_ssize_t) sep.size();
i = j = len;
while ( i >= n )
{
if ( str[i - 1] == sep[n - 1] && str.substr( i - n, n ) == sep )
{
if ( maxsplit-- <= 0 ) break;
result.push_back( str.substr( i, j - i ) );
i = j = i - n;
}
else
{
i--;
}
}
result.push_back( str.substr( 0, j ) );
reverse_strings( result );
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
#define LEFTSTRIP 0
#define RIGHTSTRIP 1
#define BOTHSTRIP 2
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
std::string do_strip( const std::string & str, int striptype, const std::string & chars )
{
Py_ssize_t len = (Py_ssize_t) str.size(), i, j, charslen = (Py_ssize_t) chars.size();
if ( charslen == 0 )
{
i = 0;
if ( striptype != RIGHTSTRIP )
{
while ( i < len && ::isspace( str[i] ) )
{
i++;
}
}
j = len;
if ( striptype != LEFTSTRIP )
{
do
{
j--;
}
while (j >= i && ::isspace(str[j]));
j++;
}
}
else
{
const char * sep = chars.c_str();
i = 0;
if ( striptype != RIGHTSTRIP )
{
while ( i < len && memchr(sep, str[i], charslen) )
{
i++;
}
}
j = len;
if (striptype != LEFTSTRIP)
{
do
{
j--;
}
while (j >= i && memchr(sep, str[j], charslen) );
j++;
}
}
if ( i == 0 && j == len )
{
return str;
}
else
{
return str.substr( i, j - i );
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
void partition( const std::string & str, const std::string & sep, std::vector< std::string > & result )
{
result.resize(3);
int index = find( str, sep );
if ( index < 0 )
{
result[0] = str;
result[1] = empty_string;
result[2] = empty_string;
}
else
{
result[0] = str.substr( 0, index );
result[1] = sep;
result[2] = str.substr( index + sep.size(), str.size() );
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
void rpartition( const std::string & str, const std::string & sep, std::vector< std::string > & result )
{
result.resize(3);
int index = rfind( str, sep );
if ( index < 0 )
{
result[0] = empty_string;
result[1] = empty_string;
result[2] = str;
}
else
{
result[0] = str.substr( 0, index );
result[1] = sep;
result[2] = str.substr( index + sep.size(), str.size() );
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
std::string strip( const std::string & str, const std::string & chars )
{
return do_strip( str, BOTHSTRIP, chars );
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
std::string lstrip( const std::string & str, const std::string & chars )
{
return do_strip( str, LEFTSTRIP, chars );
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
std::string rstrip( const std::string & str, const std::string & chars )
{
return do_strip( str, RIGHTSTRIP, chars );
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
std::string join( const std::string & str, const std::vector< std::string > & seq )
{
std::vector< std::string >::size_type seqlen = seq.size(), i;
if ( seqlen == 0 ) return empty_string;
if ( seqlen == 1 ) return seq[0];
std::string result( seq[0] );
for ( i = 1; i < seqlen; ++i )
{
result += str + seq[i];
}
return result;
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
namespace
{
/* Matches the end (direction >= 0) or start (direction < 0) of self
* against substr, using the start and end arguments. Returns
* -1 on error, 0 if not found and 1 if found.
*/
int _string_tailmatch(const std::string & self, const std::string & substr,
Py_ssize_t start, Py_ssize_t end,
int direction)
{
Py_ssize_t len = (Py_ssize_t) self.size();
Py_ssize_t slen = (Py_ssize_t) substr.size();
const char* sub = substr.c_str();
const char* str = self.c_str();
ADJUST_INDICES(start, end, len);
if (direction < 0) {
// startswith
if (start+slen > len)
return 0;
} else {
// endswith
if (end-start < slen || start > len)
return 0;
if (end-slen > start)
start = end - slen;
}
if (end-start >= slen)
return (!std::memcmp(str+start, sub, slen));
return 0;
}
}
bool endswith( const std::string & str, const std::string & suffix, int start, int end )
{
int result = _string_tailmatch(str, suffix,
(Py_ssize_t) start, (Py_ssize_t) end, +1);
//if (result == -1) // TODO: Error condition
return static_cast<bool>(result);
}
bool startswith( const std::string & str, const std::string & prefix, int start, int end )
{
int result = _string_tailmatch(str, prefix,
(Py_ssize_t) start, (Py_ssize_t) end, -1);
//if (result == -1) // TODO: Error condition
return static_cast<bool>(result);
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
bool isalnum( const std::string & str )
{
std::string::size_type len = str.size(), i;
if ( len == 0 ) return false;
if( len == 1 )
{
return ::isalnum( str[0] );
}
for ( i = 0; i < len; ++i )
{
if ( !::isalnum( str[i] ) ) return false;
}
return true;
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
bool isalpha( const std::string & str )
{
std::string::size_type len = str.size(), i;
if ( len == 0 ) return false;
if( len == 1 ) return ::isalpha( (int) str[0] );
for ( i = 0; i < len; ++i )
{
if ( !::isalpha( (int) str[i] ) ) return false;
}
return true;
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
bool isdigit( const std::string & str )
{
std::string::size_type len = str.size(), i;
if ( len == 0 ) return false;
if( len == 1 ) return ::isdigit( str[0] );
for ( i = 0; i < len; ++i )
{
if ( ! ::isdigit( str[i] ) ) return false;
}
return true;
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
bool islower( const std::string & str )
{
std::string::size_type len = str.size(), i;
if ( len == 0 ) return false;
if( len == 1 ) return ::islower( str[0] );
for ( i = 0; i < len; ++i )
{
if ( !::islower( str[i] ) ) return false;
}
return true;
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
bool isspace( const std::string & str )
{
std::string::size_type len = str.size(), i;
if ( len == 0 ) return false;
if( len == 1 ) return ::isspace( str[0] );
for ( i = 0; i < len; ++i )
{
if ( !::isspace( str[i] ) ) return false;
}
return true;
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
bool istitle( const std::string & str )
{
std::string::size_type len = str.size(), i;
if ( len == 0 ) return false;
if ( len == 1 ) return ::isupper( str[0] );
bool cased = false, previous_is_cased = false;
for ( i = 0; i < len; ++i )
{
if ( ::isupper( str[i] ) )
{
if ( previous_is_cased )
{
return false;
}
previous_is_cased = true;
cased = true;
}
else if ( ::islower( str[i] ) )
{
if (!previous_is_cased)
{
return false;
}
previous_is_cased = true;
cased = true;
}
else
{
previous_is_cased = false;
}
}
return cased;
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
bool isupper( const std::string & str )
{
std::string::size_type len = str.size(), i;
if ( len == 0 ) return false;
if( len == 1 ) return ::isupper( str[0] );
for ( i = 0; i < len; ++i )
{
if ( !::isupper( str[i] ) ) return false;
}
return true;
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
std::string capitalize( const std::string & str )
{
std::string s( str );
std::string::size_type len = s.size(), i;
if ( len > 0)
{
if (::islower(s[0])) s[0] = (char) ::toupper( s[0] );
}
for ( i = 1; i < len; ++i )
{
if (::isupper(s[i])) s[i] = (char) ::tolower( s[i] );
}
return s;
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
std::string lower( const std::string & str )
{
std::string s( str );
std::string::size_type len = s.size(), i;
for ( i = 0; i < len; ++i )
{
if ( ::isupper( s[i] ) ) s[i] = (char) ::tolower( s[i] );
}
return s;
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
std::string upper( const std::string & str )
{
std::string s( str ) ;
std::string::size_type len = s.size(), i;
for ( i = 0; i < len; ++i )
{
if ( ::islower( s[i] ) ) s[i] = (char) ::toupper( s[i] );
}
return s;
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
std::string swapcase( const std::string & str )
{
std::string s( str );
std::string::size_type len = s.size(), i;
for ( i = 0; i < len; ++i )
{
if ( ::islower( s[i] ) ) s[i] = (char) ::toupper( s[i] );
else if (::isupper( s[i] ) ) s[i] = (char) ::tolower( s[i] );
}
return s;
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
std::string title( const std::string & str )
{
std::string s( str );
std::string::size_type len = s.size(), i;
bool previous_is_cased = false;
for ( i = 0; i < len; ++i )
{
int c = s[i];
if ( ::islower(c) )
{
if ( !previous_is_cased )
{
s[i] = (char) ::toupper(c);
}
previous_is_cased = true;
}
else if ( ::isupper(c) )
{
if ( previous_is_cased )
{
s[i] = (char) ::tolower(c);
}
previous_is_cased = true;
}
else
{
previous_is_cased = false;
}
}
return s;
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
std::string translate( const std::string & str, const std::string & table, const std::string & deletechars )
{
std::string s;
std::string::size_type len = str.size(), dellen = deletechars.size();
if ( table.size() != 256 )
{
// TODO : raise exception instead
return str;
}
//if nothing is deleted, use faster code
if ( dellen == 0 )
{
s = str;
for ( std::string::size_type i = 0; i < len; ++i )
{
s[i] = table[ s[i] ];
}
return s;
}
int trans_table[256];
for ( int i = 0; i < 256; i++)
{
trans_table[i] = table[i];
}
for ( std::string::size_type i = 0; i < dellen; i++)
{
trans_table[(int) deletechars[i] ] = -1;
}
for ( std::string::size_type i = 0; i < len; ++i )
{
if ( trans_table[ (int) str[i] ] != -1 )
{
s += table[ str[i] ];
}
}
return s;
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
std::string zfill( const std::string & str, int width )
{
int len = (int)str.size();
if ( len >= width )
{
return str;
}
std::string s( str );
int fill = width - len;
s = std::string( fill, '0' ) + s;
if ( s[fill] == '+' || s[fill] == '-' )
{
s[0] = s[fill];
s[fill] = '0';
}
return s;
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
std::string ljust( const std::string & str, int width )
{
std::string::size_type len = str.size();
if ( (( int ) len ) >= width ) return str;
return str + std::string( width - len, ' ' );
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
std::string rjust( const std::string & str, int width )
{
std::string::size_type len = str.size();
if ( (( int ) len ) >= width ) return str;
return std::string( width - len, ' ' ) + str;
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
std::string center( const std::string & str, int width )
{
int len = (int) str.size();
int marg, left;
if ( len >= width ) return str;
marg = width - len;
left = marg / 2 + (marg & width & 1);
return std::string( left, ' ' ) + str + std::string( marg - left, ' ' );
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
std::string slice( const std::string & str, int start, int end )
{
ADJUST_INDICES(start, end, (int) str.size());
if ( start >= end ) return empty_string;
return str.substr( start, end - start );
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
int find( const std::string & str, const std::string & sub, int start, int end )
{
ADJUST_INDICES(start, end, (int) str.size());
std::string::size_type result = str.find( sub, start );
// If we cannot find the string, or if the end-point of our found substring is past
// the allowed end limit, return that it can't be found.
if( result == std::string::npos ||
(result + sub.size() > (std::string::size_type)end) )
{
return -1;
}
return (int) result;
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
int index( const std::string & str, const std::string & sub, int start, int end )
{
return find( str, sub, start, end );
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
int rfind( const std::string & str, const std::string & sub, int start, int end )
{
ADJUST_INDICES(start, end, (int) str.size());
std::string::size_type result = str.rfind( sub, end );
if( result == std::string::npos ||
result < (std::string::size_type)start ||
(result + sub.size() > (std::string::size_type)end))
return -1;
return (int)result;
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
int rindex( const std::string & str, const std::string & sub, int start, int end )
{
return rfind( str, sub, start, end );
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
std::string expandtabs( const std::string & str, int tabsize )
{
std::string s( str );
std::string::size_type len = str.size(), i = 0;
int offset = 0;
int j = 0;
for ( i = 0; i < len; ++i )
{
if ( str[i] == '\t' )
{
if ( tabsize > 0 )
{
int fillsize = tabsize - (j % tabsize);
j += fillsize;
s.replace( i + offset, 1, std::string( fillsize, ' ' ));
offset += fillsize - 1;
}
else
{
s.replace( i + offset, 1, empty_string );
offset -= 1;
}
}
else
{
j++;
if (str[i] == '\n' || str[i] == '\r')
{
j = 0;
}
}
}
return s;
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
int count( const std::string & str, const std::string & substr, int start, int end )
{
int nummatches = 0;
int cursor = start;
while ( 1 )
{
cursor = find( str, substr, cursor, end );
if ( cursor < 0 ) break;
cursor += (int) substr.size();
nummatches += 1;
}
return nummatches;
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
std::string replace( const std::string & str, const std::string & oldstr, const std::string & newstr, int count )
{
int sofar = 0;
int cursor = 0;
std::string s( str );
std::string::size_type oldlen = oldstr.size(), newlen = newstr.size();
cursor = find( s, oldstr, cursor );
while ( cursor != -1 && cursor <= (int)s.size() )
{
if ( count > -1 && sofar >= count )
{
break;
}
s.replace( cursor, oldlen, newstr );
cursor += (int) newlen;
if ( oldlen != 0)
{
cursor = find( s, oldstr, cursor );
}
else
{
++cursor;
}
++sofar;
}
return s;
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
void splitlines( const std::string & str, std::vector< std::string > & result, bool keepends )
{
result.clear();
std::string::size_type len = str.size(), i, j, eol;
for (i = j = 0; i < len; )
{
while (i < len && str[i] != '\n' && str[i] != '\r') i++;
eol = i;
if (i < len)
{
if (str[i] == '\r' && i + 1 < len && str[i+1] == '\n')
{
i += 2;
}
else
{
i++;
}
if (keepends)
eol = i;
}
result.push_back( str.substr( j, eol - j ) );
j = i;
}
if (j < len)
{
result.push_back( str.substr( j, len - j ) );
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
std::string mul( const std::string & str, int n )
{
// Early exits
if (n <= 0) return empty_string;
if (n == 1) return str;
std::ostringstream os;
for(int i=0; i<n; ++i)
{
os << str;
}
return os.str();
}
namespace os
{
namespace path
{
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
/// These functions are C++ ports of the python2.6 versions of os.path,
/// and come from genericpath.py, ntpath.py, posixpath.py
/// Split a pathname into drive and path specifiers.
/// Returns drivespec, pathspec. Either part may be empty.
void splitdrive_nt(std::string & drivespec, std::string & pathspec,
const std::string & p)
{
if (p.size() >= 2 && p[1] == ':')
{
std::string path = p; // In case drivespec == p
drivespec = pystring::slice(path, 0, 2);
pathspec = pystring::slice(path, 2);
}
else
{
drivespec = empty_string;
pathspec = p;
}
}
// On Posix, drive is always empty
void splitdrive_posix(std::string & drivespec, std::string & pathspec,
const std::string & path)
{
drivespec = empty_string;
pathspec = path;
}
void splitdrive(std::string & drivespec, std::string & pathspec,
const std::string & path)
{
#ifdef WINDOWS
return splitdrive_nt(drivespec, pathspec, path);
#else
return splitdrive_posix(drivespec, pathspec, path);
#endif
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
// Test whether a path is absolute
// In windows, if the character to the right of the colon
// is a forward or backslash it's absolute.
bool isabs_nt(const std::string & path)
{
std::string drivespec, pathspec;
splitdrive_nt(drivespec, pathspec, path);
if(pathspec.empty()) return false;
return ((pathspec[0] == '/') || (pathspec[0] == '\\'));
}
bool isabs_posix(const std::string & s)
{
return pystring::startswith(s, forward_slash);
}
bool isabs(const std::string & path)
{
#ifdef WINDOWS
return isabs_nt(path);
#else
return isabs_posix(path);
#endif
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
std::string abspath_nt(const std::string & path, const std::string & cwd)
{
std::string p = path;
if(!isabs_nt(p)) p = join_nt(cwd, p);
return normpath_nt(p);
}
std::string abspath_posix(const std::string & path, const std::string & cwd)
{
std::string p = path;
if(!isabs_posix(p)) p = join_posix(cwd, p);
return normpath_posix(p);
}
std::string abspath(const std::string & path, const std::string & cwd)
{
#ifdef WINDOWS
return abspath_nt(path, cwd);
#else
return abspath_posix(path, cwd);
#endif
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
std::string join_nt(const std::vector< std::string > & paths)
{
if(paths.empty()) return empty_string;
if(paths.size() == 1) return paths[0];
std::string path = paths[0];
for(unsigned int i=1; i<paths.size(); ++i)
{
std::string b = paths[i];
bool b_nts = false;
if(path.empty())
{
b_nts = true;
}
else if(isabs_nt(b))
{
// This probably wipes out path so far. However, it's more
// complicated if path begins with a drive letter:
// 1. join('c:', '/a') == 'c:/a'
// 2. join('c:/', '/a') == 'c:/a'
// But
// 3. join('c:/a', '/b') == '/b'
// 4. join('c:', 'd:/') = 'd:/'
// 5. join('c:/', 'd:/') = 'd:/'
if ((path.size() >= 2 && path[1] != ':') || (b.size() >= 2 && b[1] == ':'))
{
// Path doesnt start with a drive letter
b_nts = true;
}
// Else path has a drive letter, and b doesn't but is absolute.
else if((path.size()>3) ||
((path.size()==3) && !pystring::endswith(path, forward_slash) && !pystring::endswith(path, double_back_slash)))
{
b_nts = true;
}
}
if(b_nts)
{
path = b;
}
else
{
// Join, and ensure there's a separator.
// assert len(path) > 0
if( pystring::endswith(path, forward_slash) || pystring::endswith(path, double_back_slash))
{
if(pystring::startswith(b,forward_slash) || pystring::startswith(b,double_back_slash))
{
path += pystring::slice(b, 1);
}
else
{
path += b;
}
}
else if(pystring::endswith(path, colon))
{
path += b;
}
else if(!b.empty())
{
if(pystring::startswith(b, forward_slash) || pystring::startswith(b,double_back_slash))
{
path += b;
}
else
{
path += double_back_slash + b;
}
}
else
{
// path is not empty and does not end with a backslash,
// but b is empty; since, e.g., split('a/') produces
// ('a', ''), it's best if join() adds a backslash in
// this case.
path += double_back_slash;
}
}
}
return path;
}
// Join two or more pathname components, inserting double_back_slash as needed.
std::string join_nt(const std::string & a, const std::string & b)
{
std::vector< std::string > paths(2);
paths[0] = a;
paths[1] = b;
return join_nt(paths);
}
// Join pathnames.
// If any component is an absolute path, all previous path components
// will be discarded.
// Ignore the previous parts if a part is absolute.
// Insert a '/' unless the first part is empty or already ends in '/'.
std::string join_posix(const std::vector< std::string > & paths)
{
if(paths.empty()) return empty_string;
if(paths.size() == 1) return paths[0];
std::string path = paths[0];
for(unsigned int i=1; i<paths.size(); ++i)
{
std::string b = paths[i];
if(pystring::startswith(b, forward_slash))
{
path = b;
}
else if(path.empty() || pystring::endswith(path, forward_slash))
{
path += b;
}
else
{
path += forward_slash + b;
}
}
return path;
}
std::string join_posix(const std::string & a, const std::string & b)
{
std::vector< std::string > paths(2);
paths[0] = a;
paths[1] = b;
return join_posix(paths);
}
std::string join(const std::string & path1, const std::string & path2)
{
#ifdef WINDOWS
return join_nt(path1, path2);
#else
return join_posix(path1, path2);
#endif
}
std::string join(const std::vector< std::string > & paths)
{
#ifdef WINDOWS
return join_nt(paths);
#else
return join_posix(paths);
#endif
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
// Split a pathname.
// Return (head, tail) where tail is everything after the final slash.
// Either part may be empty
void split_nt(std::string & head, std::string & tail, const std::string & path)
{
std::string d, p;
splitdrive_nt(d, p, path);
// set i to index beyond p's last slash
int i = (int)p.size();
// walk back to find the index of the first slash from the end
while(i>0 && (p[i-1] != '\\') && (p[i-1] != '/'))
{
i = i - 1;
}
head = pystring::slice(p,0,i);
tail = pystring::slice(p,i); // now tail has no slashes
// remove trailing slashes from head, unless it's all slashes
std::string head2 = head;
while(!head2.empty() && ((pystring::slice(head2,-1) == forward_slash) ||
(pystring::slice(head2,-1) == double_back_slash)))
{
head2 = pystring::slice(head2,0,-1);
}
if(!head2.empty()) head = head2;
head = d + head;
}
// Split a path in head (everything up to the last '/') and tail (the
// rest). If the path ends in '/', tail will be empty. If there is no
// '/' in the path, head will be empty.
// Trailing '/'es are stripped from head unless it is the root.
void split_posix(std::string & head, std::string & tail, const std::string & p)
{
int i = pystring::rfind(p, forward_slash) + 1;
head = pystring::slice(p,0,i);
tail = pystring::slice(p,i);
if(!head.empty() && (head != pystring::mul(forward_slash, (int) head.size())))
{
head = pystring::rstrip(head, forward_slash);
}
}
void split(std::string & head, std::string & tail, const std::string & path)
{
#ifdef WINDOWS
return split_nt(head, tail, path);
#else
return split_posix(head, tail, path);
#endif
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
std::string basename_nt(const std::string & path)
{
std::string head, tail;
split_nt(head, tail, path);
return tail;
}
std::string basename_posix(const std::string & path)
{
std::string head, tail;
split_posix(head, tail, path);
return tail;
}
std::string basename(const std::string & path)
{
#ifdef WINDOWS
return basename_nt(path);
#else
return basename_posix(path);
#endif
}
std::string dirname_nt(const std::string & path)
{
std::string head, tail;
split_nt(head, tail, path);
return head;
}
std::string dirname_posix(const std::string & path)
{
std::string head, tail;
split_posix(head, tail, path);
return head;
}
std::string dirname(const std::string & path)
{
#ifdef WINDOWS
return dirname_nt(path);
#else
return dirname_posix(path);
#endif
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
// Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A\B.
std::string normpath_nt(const std::string & p)
{
std::string path = p;
path = pystring::replace(path, forward_slash,double_back_slash);
std::string prefix;
splitdrive_nt(prefix, path, path);
// We need to be careful here. If the prefix is empty, and the path starts
// with a backslash, it could either be an absolute path on the current
// drive (\dir1\dir2\file) or a UNC filename (\\server\mount\dir1\file). It
// is therefore imperative NOT to collapse multiple backslashes blindly in
// that case.
// The code below preserves multiple backslashes when there is no drive
// letter. This means that the invalid filename \\\a\b is preserved
// unchanged, where a\\\b is normalised to a\b. It's not clear that there
// is any better behaviour for such edge cases.
if(prefix.empty())
{
// No drive letter - preserve initial backslashes
while(pystring::slice(path,0,1) == double_back_slash)
{
prefix = prefix + double_back_slash;
path = pystring::slice(path,1);
}
}
else
{
// We have a drive letter - collapse initial backslashes
if(pystring::startswith(path, double_back_slash))
{
prefix = prefix + double_back_slash;
path = pystring::lstrip(path, double_back_slash);
}
}
std::vector<std::string> comps;
pystring::split(path, comps, double_back_slash);
int i = 0;
while(i<(int)comps.size())
{
if(comps[i].empty() || comps[i] == dot)
{
comps.erase(comps.begin()+i);
}
else if(comps[i] == double_dot)
{
if(i>0 && comps[i-1] != double_dot)
{
comps.erase(comps.begin()+i-1, comps.begin()+i+1);
i -= 1;
}
else if(i == 0 && pystring::endswith(prefix, double_back_slash))
{
comps.erase(comps.begin()+i);
}
else
{
i += 1;
}
}
else
{
i += 1;
}
}
// If the path is now empty, substitute '.'
if(prefix.empty() && comps.empty())
{
comps.push_back(dot);
}
return prefix + pystring::join(double_back_slash, comps);
}
// Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
// It should be understood that this may change the meaning of the path
// if it contains symbolic links!
// Normalize path, eliminating double slashes, etc.
std::string normpath_posix(const std::string & p)
{
if(p.empty()) return dot;
std::string path = p;
int initial_slashes = pystring::startswith(path, forward_slash) ? 1 : 0;
// POSIX allows one or two initial slashes, but treats three or more
// as single slash.
if (initial_slashes && pystring::startswith(path, double_forward_slash)
&& !pystring::startswith(path, triple_forward_slash))
initial_slashes = 2;
std::vector<std::string> comps, new_comps;
pystring::split(path, comps, forward_slash);
for(unsigned int i=0; i<comps.size(); ++i)
{
std::string comp = comps[i];
if(comp.empty() || comp == dot)
continue;
if( (comp != double_dot) || ((initial_slashes == 0) && new_comps.empty()) ||
(!new_comps.empty() && new_comps[new_comps.size()-1] == double_dot))
{
new_comps.push_back(comp);
}
else if (!new_comps.empty())
{
new_comps.pop_back();
}
}
path = pystring::join(forward_slash, new_comps);
if (initial_slashes > 0)
path = pystring::mul(forward_slash, initial_slashes) + path;
if(path.empty()) return dot;
return path;
}
std::string normpath(const std::string & path)
{
#ifdef WINDOWS
return normpath_nt(path);
#else
return normpath_posix(path);
#endif
}
//////////////////////////////////////////////////////////////////////////////////////////////
///
///
// Split the extension from a pathname.
// Extension is everything from the last dot to the end, ignoring
// leading dots. Returns "(root, ext)"; ext may be empty.
// It is always true that root + ext == p
void splitext_generic(std::string & root, std::string & ext,
const std::string & p,
const std::string & sep,
const std::string & altsep,
const std::string & extsep)
{
int sepIndex = pystring::rfind(p, sep);
if(!altsep.empty())
{
int altsepIndex = pystring::rfind(p, altsep);
sepIndex = std::max(sepIndex, altsepIndex);
}
int dotIndex = pystring::rfind(p, extsep);
if(dotIndex > sepIndex)
{
// Skip all leading dots
int filenameIndex = sepIndex + 1;
while(filenameIndex < dotIndex)
{
if(pystring::slice(p,filenameIndex) != extsep)
{
root = pystring::slice(p, 0, dotIndex);
ext = pystring::slice(p, dotIndex);
return;
}
filenameIndex += 1;
}
}
root = p;
ext = empty_string;
}
void splitext_nt(std::string & root, std::string & ext, const std::string & path)
{
return splitext_generic(root, ext, path,
double_back_slash, forward_slash, dot);
}
void splitext_posix(std::string & root, std::string & ext, const std::string & path)
{
return splitext_generic(root, ext, path,
forward_slash, empty_string, dot);
}
void splitext(std::string & root, std::string & ext, const std::string & path)
{
#ifdef WINDOWS
return splitext_nt(root, ext, path);
#else
return splitext_posix(root, ext, path);
#endif
}
} // namespace path
} // namespace os
}//namespace pystring
|