DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (379a2370cd89)

VCS Links

Line Code
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is mozilla.org code.
 *
 * The Initial Developer of the Original Code is
 * Netscape Communications Corporation.
 * Portions created by the Initial Developer are Copyright (C) 1998
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either of the GNU General Public License Version 2 or later (the "GPL"),
 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

#include "msgCore.h"    // precompiled header...
#include "prlog.h"

#include "MailNewsTypes.h"
#include "nsMsgKeySet.h"
#include "prprf.h"
#include "prmem.h"
#include "nsTArray.h"
#include "nsMemory.h"
#include <ctype.h>

#if defined(DEBUG_seth_) || defined(DEBUG_sspitzer_)
#define DEBUG_MSGKEYSET 1
#endif

/* A compressed encoding for sets of article.  This is usually for lines from
   the newsrc, which have article lists like

   1-29627,29635,29658,32861-32863

   so the data has these properties:

   - strictly increasing
   - large subsequences of monotonically increasing ranges
   - gaps in the set are usually small, but not always
   - consecutive ranges tend to be large

   The biggest win is to run-length encode the data, storing ranges as two
   numbers (start+length or start,end). We could also store each number as a
   delta from the previous number for further compression, but that gets kind
   of tricky, since there are no guarentees about the sizes of the gaps, and
   we'd have to store variable-length words.

   Current data format:

   DATA := SIZE [ CHUNK ]*
   CHUNK := [ RANGE | VALUE ]
   RANGE := -LENGTH START
   START := VALUE
   LENGTH := PRInt32
   VALUE := a literal positive integer, for now
   it could also be an offset from the previous value.
   LENGTH could also perhaps be a less-than-32-bit quantity,
   at least most of the time.

   Lengths of CHUNKs are stored negative to distinguish the beginning of
   a chunk from a literal: negative means two-word sequence, positive
   means one-word sequence.

   0 represents a literal 0, but should not occur, and should never occur
   except in the first position.

   A length of -1 won't occur either, except temporarily - a sequence of
   two elements is represented as two literals, since they take up the same
   space.

   Another optimization we make is to notice that we typically ask the
   question ``is N a member of the set'' for increasing values of N. So the
   set holds a cache of the last value asked for, and can simply resume the
   search from there.  */

nsMsgKeySet::nsMsgKeySet(/* MSG_NewsHost* host*/)
{
  MOZ_COUNT_CTOR(nsMsgKeySet);
  m_cached_value = -1;
  m_cached_value_index = 0;
  m_length = 0;
  m_data_size = 10;
  m_data = (PRInt32 *) PR_Malloc (sizeof (PRInt32) * m_data_size);
#ifdef NEWSRC_DOES_HOST_STUFF
  m_host = host;
#endif
}


nsMsgKeySet::~nsMsgKeySet()
{
  MOZ_COUNT_DTOR(nsMsgKeySet);
  PR_FREEIF(m_data);
}


bool nsMsgKeySet::Grow()
{
  PRInt32 new_size;
  PRInt32 *new_data;
  new_size = m_data_size * 2;
  new_data = (PRInt32 *) PR_REALLOC (m_data, sizeof (PRInt32) * new_size);
  if (! new_data)
    return PR_FALSE;
  m_data_size = new_size;
  m_data = new_data;
  return PR_TRUE;
}


nsMsgKeySet::nsMsgKeySet(const char* numbers /* , MSG_NewsHost* host */)
{
  PRInt32 *head, *tail, *end;
    MOZ_COUNT_CTOR(nsMsgKeySet);

#ifdef NEWSRC_DOES_HOST_STUFF
  m_host = host;
#endif
  m_cached_value = -1;
  m_cached_value_index = 0;
  m_length = 0;
  m_data_size = 10;
  m_data = (PRInt32 *) PR_Malloc (sizeof (PRInt32) * m_data_size);
  if (!m_data) return;

  head = m_data;
  tail = head;
  end = head + m_data_size;

  if(!numbers) {
    return;
  }

  while (isspace (*numbers)) numbers++;
  while (*numbers) {
    PRInt32 from = 0;
    PRInt32 to;

    if (tail >= end - 4) {
      /* out of room! */
      PRInt32 tailo = tail - head;
      if (!Grow()) {
        PR_FREEIF(m_data);
        return;
      }
      /* data may have been relocated */
      head = m_data;
      tail = head + tailo;
      end = head + m_data_size;
    }

    while (isspace(*numbers)) numbers++;
    if (*numbers && !isdigit(*numbers)) {
      break;      /* illegal character */
    }
    while (isdigit (*numbers)) {
      from = (from * 10) + (*numbers++ - '0');
    }
    while (isspace(*numbers)) numbers++;
    if (*numbers != '-') {
      to = from;
    } else {
      to = 0;
      numbers++;
      while (*numbers >= '0' && *numbers <= '9')
        to = (to * 10) + (*numbers++ - '0');
      while (isspace(*numbers)) numbers++;
    }

    if (to < from) to = from; /* illegal */

    /* This is a hack - if the newsrc file specifies a range 1-x as
       being read, we internally pretend that article 0 is read as well.
       (But if only 2-x are read, then 0 is not read.)  This is needed
       because some servers think that article 0 is an article (I think)
       but some news readers (including Netscape 1.1) choke if the .newsrc
       file has lines beginning with 0...   ### */
    if (from == 1) from = 0;

    if (to == from) {
      /* Write it as a literal */
      *tail = from;
      tail++;
    } else /* Write it as a range. */ {
      *tail = -(to - from);
      tail++;
      *tail = from;
      tail++;
    }

    while (*numbers == ',' || isspace(*numbers)) {
      numbers++;
    }
  }

  m_length = tail - head; /* size of data */
}



nsMsgKeySet*
nsMsgKeySet::Create(/*MSG_NewsHost* host*/)
{
  nsMsgKeySet* set = new nsMsgKeySet(/* host */);
  if (set && set->m_data == NULL) {
    delete set;
    set = NULL;
  }
  return set;
}


nsMsgKeySet*
nsMsgKeySet::Create(const char* value /* , MSG_NewsHost* host */)
{
#ifdef DEBUG_MSGKEYSET
    printf("create from %s\n",value);
#endif

  nsMsgKeySet* set = new nsMsgKeySet(value /* , host */);
  if (set && set->m_data == NULL) {
    delete set;
    set = NULL;
  }
  return set;
}



/* Returns the lowest non-member of the set greater than 0.
 */
PRInt32
nsMsgKeySet::FirstNonMember ()
{
  if (m_length <= 0) {
    return 1;
  } else if(m_data[0] < 0 && m_data[1] != 1 && m_data[1] != 0) {
    /* first range not equal to 0 or 1, always return 1 */
    return 1;
  } else if (m_data[0] < 0) {
    /* it's a range */
    /* If there is a range [N-M] we can presume that M+1 is not in the
       set. */
    return (m_data[1] - m_data[0] + 1);
  } else {
    /* it's a literal */
    if (m_data[0] == 1) {
      /* handle "1,..." */
      if (m_length > 1 && m_data[1] == 2) {
        /* This is "1,2,M-N,..." or "1,2,M,..."  where M >= 4.  Note
           that M will never be 3, because in that case we would have
           started with a range: "1-3,..." */
        return 3;
      } else {
        return 2;              /* handle "1,M-N,.." or "1,M,..."
                      where M >= 3; */
      }
    }
    else if (m_data[0] == 0) {
      /* handle "0,..." */
      if (m_length > 1 && m_data[1] == 1) {
        /* this is 0,1, (see above) */
        return 2;
      }
      else {
        return 1;
      }

    } else {
      /* handle "M,..." where M >= 2. */
      return 1;
    }
  }
}


nsresult 
nsMsgKeySet::Output(char **outputStr)
{
  NS_ENSURE_ARG(outputStr);
  PRInt32 size;
  PRInt32 *head;
  PRInt32 *tail;
  PRInt32 *end;
  PRInt32 s_size;
  char *s_head;
  char *s, *s_end;
  PRInt32 last_art = -1;

  *outputStr = nsnull;

  size = m_length;
  head = m_data;
  tail = head;
  end = head + size;

  s_size = (size * 12) + 10;  // dmb - try to make this allocation get used at least once.
  s_head = (char *) nsMemory::Alloc(s_size);
  if (! s_head) return NS_ERROR_OUT_OF_MEMORY;

  s_head[0] = '\0';      // otherwise, s_head will contain garbage.
  s = s_head;
  s_end = s + s_size;

  while (tail < end) {
    PRInt32 from;
    PRInt32 to;

    if (s > (s_end - (12 * 2 + 10))) { /* 12 bytes for each number (enough
                        for "2147483647" aka 2^31-1),
                        plus 10 bytes of slop. */
      PRInt32 so = s - s_head;
      s_size += 200;
      char* tmp = (char *) nsMemory::Alloc(s_size);
      if (tmp) PL_strcpy(tmp, s_head);
      nsMemory::Free(s_head);
      s_head = tmp;
      if (!s_head) return NS_ERROR_OUT_OF_MEMORY;
      s = s_head + so;
      s_end = s_head + s_size;
    }

    if (*tail < 0) {
      /* it's a range */
      from = tail[1];
      to = from + (-(tail[0]));
      tail += 2;
    }
    else /* it's a literal */
      {
        from = *tail;
        to = from;
        tail++;
      }
    if (from == 0) {
      from = 1;        /* See 'hack' comment above  ### */
    }
    if (from <= last_art) from = last_art + 1;
    if (from <= to) {
      if (from < to) {
        PR_snprintf(s, s_end - s, "%lu-%lu,", from, to);
      } else {
        PR_snprintf(s, s_end - s, "%lu,", from);
      }
      s += PL_strlen(s);
      last_art = to;
    }
  }
  if (last_art >= 0) {
    s--;              /* Strip off the last ',' */
  }

  *s = 0;

  *outputStr = s_head;
  return NS_OK;
}

PRInt32 
nsMsgKeySet::GetLastMember()
{
  if (m_length > 1)
  {
    PRInt32 nextToLast = m_data[m_length - 2];
    if (nextToLast < 0)  // is range at end?
    {
      PRInt32 last = m_data[m_length - 1];
      return (-nextToLast + last - 1);
    }
    else  // no, so last number must be last member
    {
      return m_data[m_length - 1];
    }
  }
  else if (m_length == 1)
    return m_data[0];  // must be only 1 read.
  else
    return 0;
}

void nsMsgKeySet::SetLastMember(PRInt32 newHighWaterMark)
{
  if (newHighWaterMark < GetLastMember())
  {
    while (PR_TRUE)
    {
      if (m_length > 1)
      {
        PRInt32 nextToLast = m_data[m_length - 2];
        PRInt32 curHighWater;
        if (nextToLast < 0)  // is range at end?
        {
          PRInt32 rangeStart = m_data[m_length - 1];
          PRInt32 rangeLength = -nextToLast;
          curHighWater = (rangeLength + rangeStart - 1);
          if (curHighWater > newHighWaterMark)
          {
            if (rangeStart > newHighWaterMark)  
            {
              m_length -= 2;  // throw away whole range
            }
            else if (rangeStart == newHighWaterMark)
            {
              // turn range into single element.
              m_data[m_length - 2] = newHighWaterMark;
              m_length--;
              break;
            }
            else  // just shorten range
            {
              m_data[m_length - 2] = -(newHighWaterMark - rangeStart);
              break;
            }
          }
          else {
            // prevent the infinite loop
            // see bug #13062
            break;
          }  
        }
        else if (m_data[m_length - 1] > newHighWaterMark)  // no, so last number must be last member
        {
          m_length--;
        }
        else
          break;
      }
      else 
        break;
    }
    // well, the whole range is probably invalid, because the server probably re-ordered ids, 
    // but what can you do?
#ifdef NEWSRC_DOES_HOST_STUFF
    if (m_host) 
      m_host->MarkDirty();
#endif
  }
}

PRInt32 
nsMsgKeySet::GetFirstMember()
{
  if (m_length > 1)
  {
    PRInt32 first = m_data[0];
    if (first < 0)  // is range at start?
    {
      PRInt32 second = m_data[1];
      return (second);
    }
    else  // no, so first number must be first member
    {
      return m_data[0];
    }
  }
  else if (m_length == 1)
    return m_data[0];  // must be only 1 read.
  else
    return 0;
}

/* Re-compresses a `nsMsgKeySet' object.

   The assumption is made that the `nsMsgKeySet' is syntactically correct
   (all ranges have a length of at least 1, and all values are non-
   decreasing) but will optimize the compression, for example, merging
   consecutive literals or ranges into one range.

   Returns PR_TRUE if successful, PR_FALSE if there wasn't enough memory to
   allocate scratch space.

   #### This should be changed to modify the buffer in place.

   Also note that we never call Optimize() unless we actually changed
   something, so it's a great place to tell the MSG_NewsHost* that something
   changed.
   */
bool
nsMsgKeySet::Optimize()
{
  PRInt32 input_size;
  PRInt32 output_size;
  PRInt32 *input_tail;
  PRInt32 *output_data;
  PRInt32 *output_tail;
  PRInt32 *input_end;
  PRInt32 *output_end;

  input_size = m_length;
  output_size = input_size + 1;
  input_tail = m_data;
  output_data = (PRInt32 *) PR_Malloc (sizeof (PRInt32) * output_size);
  if (!output_data) return PR_FALSE;

  output_tail = output_data;
  input_end = input_tail + input_size;
  output_end = output_data + output_size;

  /* We're going to modify the set, so invalidate the cache. */
  m_cached_value = -1;

  while (input_tail < input_end) {
    PRInt32 from, to;
    bool range_p = (*input_tail < 0);

    if (range_p) {
      /* it's a range */
      from = input_tail[1];
      to = from + (-(input_tail[0]));

      /* Copy it over */
      *output_tail++ = *input_tail++;
      *output_tail++ = *input_tail++;        
    } else {
      /* it's a literal */
      from = *input_tail;
      to = from;

      /* Copy it over */
      *output_tail++ = *input_tail++;
    }
    NS_ASSERTION(output_tail < output_end, "invalid end of output string");
    if (output_tail >= output_end) {
      PR_Free(output_data);
      return PR_FALSE;
    }

    /* As long as this chunk is followed by consecutive chunks,
       keep extending it. */
    while (input_tail < input_end &&
         ((*input_tail > 0 && /* literal... */
         *input_tail == to + 1) || /* ...and consecutive, or */
        (*input_tail <= 0 && /* range... */
         input_tail[1] == to + 1)) /* ...and consecutive. */
         ) {
      if (! range_p) {
        /* convert the literal to a range. */
        output_tail++;
        output_tail [-2] = 0;
        output_tail [-1] = from;
        range_p = PR_TRUE;
      }

      if (*input_tail > 0) { /* literal */
        output_tail[-2]--; /* increase length by 1 */
        to++;
        input_tail++;
      } else {
        PRInt32 L2 = (- *input_tail) + 1;
        output_tail[-2] -= L2; /* increase length by N */
        to += L2;
        input_tail += 2;
      }
    }
  }

  PR_Free (m_data);
  m_data = output_data;
  m_data_size = output_size;
  m_length = output_tail - output_data;

  /* One last pass to turn [N - N+1] into [N, N+1]. */
  output_tail = output_data;
  output_end = output_tail + m_length;
  while (output_tail < output_end) {
    if (*output_tail < 0) {
      /* it's a range */
      if (output_tail[0] == -1) {
        output_tail[0] = output_tail[1];
        output_tail[1]++;
      }
      output_tail += 2;
    } else {
      /* it's a literal */
      output_tail++;
    }
  }

#ifdef NEWSRC_DOES_HOST_STUFF
  if (m_host) m_host->MarkDirty();
#endif
  return PR_TRUE;
}



bool
nsMsgKeySet::IsMember(PRInt32 number)
{
  bool value = false;
  PRInt32 size;
  PRInt32 *head;
  PRInt32 *tail;
  PRInt32 *end;

  size = m_length;
  head = m_data;
  tail = head;
  end = head + size;

  /* If there is a value cached, and that value is smaller than the
     value we're looking for, skip forward that far. */
  if (m_cached_value > 0 &&
    m_cached_value < number) {
    tail += m_cached_value_index;
  }

  while (tail < end) {
    if (*tail < 0) {
      /* it's a range */
      PRInt32 from = tail[1];
      PRInt32 to = from + (-(tail[0]));
      if (from > number) {
        /* This range begins after the number - we've passed it. */
        value = PR_FALSE;
        goto DONE;
      } else if (to >= number) {
        /* In range. */
        value = PR_TRUE;
        goto DONE;
      } else {
        tail += 2;
      }
    }
    else {
      /* it's a literal */
      if (*tail == number) {
        /* bang */
        value = PR_TRUE;
        goto DONE;
      } else if (*tail > number) {
        /* This literal is after the number - we've passed it. */
        value = PR_FALSE;
        goto DONE;
      } else {
        tail++;
      }
    }
  }

DONE:
  /* Store the position of this chunk for next time. */
  m_cached_value = number;
  m_cached_value_index = tail - head;

  return value;
}


int
nsMsgKeySet::Add(PRInt32 number)
{
  PRInt32 size;
  PRInt32 *head;
  PRInt32 *tail;
  PRInt32 *end;

#ifdef DEBUG_MSGKEYSET
    printf("add %d\n",number);
#endif
    
  size = m_length;
  head = m_data;
  tail = head;
  end = head + size;

  NS_ASSERTION (number >= 0, "can't have negative items");
  if (number < 0)
    return 0;

  /* We're going to modify the set, so invalidate the cache. */
  m_cached_value = -1;

  while (tail < end) {
    if (*tail < 0) {
      /* it's a range */
      PRInt32 from = tail[1];
      PRInt32 to = from + (-(tail[0]));

      if (from <= number && to >= number) {
        /* This number is already present - we don't need to do
           anything. */
        return 0;
      }

      if (to > number) {
        /* We have found the point before which the new number
           should be inserted. */
        break;
      }

      tail += 2;
    } else {
      /* it's a literal */
      if (*tail == number) {
        /* This number is already present - we don't need to do
           anything. */
        return 0;
      }

      if (*tail > number) {
        /* We have found the point before which the new number
           should be inserted. */
        break;
      }

      tail++;
    }
  }

  /* At this point, `tail' points to a position in the set which represents
     a value greater than `new'; or it is at `end'. In the interest of
     avoiding massive duplication of code, simply insert a literal here and
     then run the optimizer.
     */
  int mid = (tail - head); 

  if (m_data_size <= m_length + 1) {
    int endo = end - head;
    if (!Grow()) {
      return NS_ERROR_OUT_OF_MEMORY;
    }
    head = m_data;
    end = head + endo;
  }

  if (tail == end) {
    /* at the end */
    /* Add a literal to the end. */
    m_data[m_length++] = number;
  } else {
    /* need to insert (or edit) in the middle */
    PRInt32 i;
    for (i = size; i > mid; i--) {
      m_data[i] = m_data[i-1];
    }
    m_data[i] = number;
    m_length++;
  }

  Optimize();
  return 1;
}



int
nsMsgKeySet::Remove(PRInt32 number)
{
  PRInt32 size;
  PRInt32 *head;
  PRInt32 *tail;
  PRInt32 *end;
#ifdef DEBUG_MSGKEYSET
    printf("remove %d\n",number);
#endif

  size = m_length;
  head = m_data;
  tail = head;
  end = head + size;

  // **** I am not sure this is a right thing to comment the following
  // statements out. The reason for this is due to the implementation of
  // offline save draft and template. We use faked UIDs (negative ids) for
  // offline draft and template in order to distinguish them from real
  // UID. David I need your help here. **** jt

  // PR_ASSERT(number >= 0);
  // if (number < 0) {
  //  return -1;
  /// }

  /* We're going to modify the set, so invalidate the cache. */
  m_cached_value = -1;

  while (tail < end) {
    PRInt32 mid = (tail - m_data);

    if (*tail < 0) {
      /* it's a range */
      PRInt32 from = tail[1];
      PRInt32 to = from + (-(tail[0]));

      if (number < from || number > to) {
        /* Not this range */
        tail += 2;
        continue;
      }

      if (to == from + 1) {
        /* If this is a range [N - N+1] and we are removing M
           (which must be either N or N+1) replace it with a
           literal. This reduces the length by 1. */
        m_data[mid] = (number == from ? to : from);
        while (++mid < m_length) {
          m_data[mid] = m_data[mid+1];
        }
        m_length--;
        Optimize();
        return 1;
      } else if (to == from + 2) {
        /* If this is a range [N - N+2] and we are removing M,
           replace it with the literals L,M (that is, either
           (N, N+1), (N, N+2), or (N+1, N+2). The overall
           length remains the same. */
        m_data[mid] = from;
        m_data[mid+1] = to;
        if (from == number) {
          m_data[mid] = from+1;
        } else if (to == number) {
          m_data[mid+1] = to-1;
        }
        Optimize();
        return 1;
      } else if (from == number) {
        /* This number is at the beginning of a long range (meaning a
           range which will still be long enough to remain a range.)
           Increase start and reduce length of the range. */
        m_data[mid]++;
        m_data[mid+1]++;
        Optimize();
        return 1;
      } else if (to == number) {
        /* This number is at the end of a long range (meaning a range
           which will still be long enough to remain a range.)
           Just decrease the length of the range. */
        m_data[mid]++;
        Optimize();
        return 1;
      } else {
        /* The number being deleted is in the middle of a range which
           must be split. This increases overall length by 2.
           */
        PRInt32 i;
        int endo = end - head;
        if (m_data_size - m_length <= 2) {
          if (!Grow()) return NS_ERROR_OUT_OF_MEMORY;
        }
        head = m_data;
        end = head + endo;

        for (i = m_length + 2; i > mid + 2; i--) {
          m_data[i] = m_data[i-2];
        }

        m_data[mid] = (- (number - from - 1));
        m_data[mid+1] = from;
        m_data[mid+2] = (- (to - number - 1));
        m_data[mid+3] = number + 1;
        m_length += 2;

        /* Oops, if we've ended up with a range with a 0 length,
           which is illegal, convert it to a literal, which reduces
           the overall length by 1. */
        if (m_data[mid] == 0) {
          /* first range */
          m_data[mid] = m_data[mid+1];
          for (i = mid + 1; i < m_length; i++) {
            m_data[i] = m_data[i+1];
          }
          m_length--;
        }
        if (m_data[mid+2] == 0) {
          /* second range */
          m_data[mid+2] = m_data[mid+3];
          for (i = mid + 3; i < m_length; i++) {
            m_data[i] = m_data[i+1];
          }
          m_length--;
        }
        Optimize();
        return 1;
      }
    } else {
      /* it's a literal */
      if (*tail != number) {
        /* Not this literal */
        tail++;
        continue;
      }

      /* Excise this literal. */
      m_length--;
      while (mid < m_length) {
        m_data[mid] = m_data[mid+1];
        mid++;
      }
      Optimize();
      return 1;
    }
  }

  /* It wasn't here at all. */
  return 0;
}


static PRInt32*
msg_emit_range(PRInt32* tmp, PRInt32 a, PRInt32 b)
{
  if (a == b) {
    *tmp++ = a;
  } else {
    NS_ASSERTION(a < b && a >= 0, "range is out of order");
    *tmp++ = -(b - a);
    *tmp++ = a;
  }
  return tmp;
}


int
nsMsgKeySet::AddRange(PRInt32 start, PRInt32 end)
{
  PRInt32 tmplength;
  PRInt32* tmp;
  PRInt32* in;
  PRInt32* out;
  PRInt32* tail;
  PRInt32 a;
  PRInt32 b;
  bool didit = false;

  /* We're going to modify the set, so invalidate the cache. */
  m_cached_value = -1;

  NS_ASSERTION(start <= end, "invalid range");
  if (start > end) return -1;

  if (start == end) {
    return Add(start);
  }

  tmplength = m_length + 2;
  tmp = (PRInt32*) PR_Malloc(sizeof(PRInt32) * tmplength);

  if (!tmp) return NS_ERROR_OUT_OF_MEMORY;

  in = m_data;
  out = tmp;
  tail = in + m_length;

#define EMIT(x, y) out = msg_emit_range(out, x, y)

  while (in < tail) {
    // Set [a,b] to be this range.
    if (*in < 0) {
      b = - *in++;
      a = *in++;
      b += a;
    } else {
      a = b = *in++;
    }

    if (a <= start && b >= end) {
      // We already have the entire range marked.
      PR_Free(tmp);
      return 0;
    }
    if (start > b + 1) {
      // No overlap yet.
      EMIT(a, b);
    } else if (end < a - 1) {
      // No overlap, and we passed it.
      EMIT(start, end);
      EMIT(a, b);
      didit = PR_TRUE;
      break;
    } else {
      // The ranges overlap.  Suck this range into our new range, and
      // keep looking for other ranges that might overlap.
      start = start < a ? start : a;
      end = end > b ? end : b;
    }
  }
  if (!didit) EMIT(start, end);
  while (in < tail) {
    *out++ = *in++;
  }

#undef EMIT

  PR_Free(m_data);
  m_data = tmp;
  m_length = out - tmp;
  m_data_size = tmplength;
#ifdef NEWSRC_DOES_HOST_STUFF
  if (m_host) m_host->MarkDirty();
#endif
  return 1;
}

PRInt32
nsMsgKeySet::CountMissingInRange(PRInt32 range_start, PRInt32 range_end)
{
  PRInt32 count;
  PRInt32 *head;
  PRInt32 *tail;
  PRInt32 *end;

  NS_ASSERTION (range_start >= 0 && range_end >= 0 && range_end >= range_start, "invalid range");
  if (range_start < 0 || range_end < 0 || range_end < range_start) return -1;

  head = m_data;
  tail = head;
  end = head + m_length;

  count = range_end - range_start + 1;

  while (tail < end) {
    if (*tail < 0) {
      /* it's a range */
      PRInt32 from = tail[1];
      PRInt32 to = from + (-(tail[0]));
      if (from < range_start) from = range_start;
      if (to > range_end) to = range_end;

      if (to >= from)
        count -= (to - from + 1);

      tail += 2;
    } else {
      /* it's a literal */
      if (*tail >= range_start && *tail <= range_end) count--;
      tail++;
    }
    NS_ASSERTION (count >= 0, "invalid count");
  }
  return count;
}


int 
nsMsgKeySet::FirstMissingRange(PRInt32 min, PRInt32 max,
                  PRInt32* first, PRInt32* last)
{
  PRInt32 size;
  PRInt32 *head;
  PRInt32 *tail;
  PRInt32 *end;
  PRInt32 from = 0;
  PRInt32 to = 0;
  PRInt32 a;
  PRInt32 b;

  NS_ASSERTION(first && last, "invalid parameter");
  if (!first || !last) return -1;

  *first = *last = 0;

  NS_ASSERTION(min <= max && min > 0, "invalid min or max param");
  if (min > max || min <= 0) return -1;

  size = m_length;
  head = m_data;
  tail = head;
  end = head + size;

  while (tail < end) {
    a = to + 1;
    if (*tail < 0) {      /* We got a range. */
      from = tail[1];
      to = from + (-(tail[0]));
      tail += 2;
    } else {
      from = to = tail[0];
      tail++;
    }
    b = from - 1;
    /* At this point, [a,b] is the range of unread articles just before
       the current range of read articles [from,to].  See if this range
       intersects the [min,max] range we were given. */
    if (a > max) return 0;  /* It's hopeless; there are none. */
    if (a <= b && b >= min) {
      /* Ah-hah!  We found an intersection. */
      *first = a > min ? a : min;
      *last = b < max ? b : max;
      return 0;
    }
  } 
  /* We found no holes in the newsrc that overlaps the range, nor did we hit
     something read beyond the end of the range.  So, the great infinite
     range of unread articles at the end of any newsrc line intersects the
     range we want, and we just need to return that. */
  a = to + 1;
  *first = a > min ? a : min;
  *last = max;
  return 0;
}

// I'm guessing we didn't include this because we didn't think we're going
// to need it. I'm not so sure. I'm putting it in for now.
int 
nsMsgKeySet::LastMissingRange(PRInt32 min, PRInt32 max,
                  PRInt32* first, PRInt32* last)
{
  PRInt32 size;
  PRInt32 *head;
  PRInt32 *tail;
  PRInt32 *end;
  PRInt32 from = 0;
  PRInt32 to = 0;
  PRInt32 a;
  PRInt32 b;

  NS_ASSERTION(first && last, "invalid null param");
  if (!first || !last) return -1;

  *first = *last = 0;


  NS_ASSERTION(min <= max && min > 0, "invalid min or max");
  if (min > max || min <= 0) return -1;

  size = m_length;
  head = m_data;
  tail = head;
  end = head + size;

  while (tail < end) {
  a = to + 1;
  if (*tail < 0) {      /* We got a range. */
    from = tail[1];
    to = from + (-(tail[0]));
    tail += 2;
  } else {
    from = to = tail[0];
    tail++;
  }
  b = from - 1;
  /* At this point, [a,b] is the range of unread articles just before
     the current range of read articles [from,to].  See if this range
     intersects the [min,max] range we were given. */
  if (a > max) return 0;  /* We're done.  If we found something, it's already
                 sitting in [*first,*last]. */
  if (a <= b && b >= min) {
    /* Ah-hah!  We found an intersection. */
    *first = a > min ? a : min;
    *last = b < max ? b : max;
    /* Continue on, looking for a later range. */
  }
  }
  if (to < max) {
  /* The great infinite range of unread articles at the end of any newsrc
     line intersects the range we want, and we just need to return that. */
  a = to + 1;
  *first = a > min ? a : min;
  *last = max;
  }
  return 0;
}

/**
 * Fill the passed in aArray with the keys in the message key set.
 */
nsresult
nsMsgKeySet::ToMsgKeyArray(nsTArray<nsMsgKey> &aArray)
{
    PRInt32 size;
    PRInt32 *head;
    PRInt32 *tail;
    PRInt32 *end;
    PRInt32 last_art = -1;

    size = m_length;
    head = m_data;
    tail = head;
    end = head + size;

    while (tail < end) {
        PRInt32 from;
        PRInt32 to;

        if (*tail < 0) {
            /* it's a range */
            from = tail[1];
            to = from + (-(tail[0]));
            tail += 2;
        }
        else /* it's a literal */
            {
                from = *tail;
                to = from;
                tail++;
            }
        // The horrible news-hack used to adjust from to 1 if it was zero right
        // here, but there is no longer a consumer of this method with that
        // broken use-case.
        if (from <= last_art) from = last_art + 1;
        if (from <= to) {
            if (from < to) {
                for (PRInt32 i = from; i <= to ; ++i ) {
                    aArray.AppendElement(i);
                }
            } else {
                aArray.AppendElement(from);
            }
            last_art = to;
        }
    }

    return NS_OK;
}


#ifdef DEBUG /* A lot of test cases for the above */

#define countof(x) (sizeof(x) / sizeof(*(x)))

void
nsMsgKeySet::test_decoder (const char *string)
{
  nsMsgKeySet set(string /* , NULL */);
  char* tmp;
  set.Output(&tmp);
  printf ("\t\"%s\"\t--> \"%s\"\n", string, tmp);
  nsMemory::Free(tmp);
}


#define START(STRING) \
  string = STRING;    \
  if (!(set = nsMsgKeySet::Create(string))) abort ()

#define FROB(N,PUSHP)                  \
  i = N;                        \
  if (!(NS_SUCCEEDED(set->Output(&s)))) abort ();          \
  printf ("%3lu: %-58s %c %3lu =\n", (unsigned long)set->m_length, s,  \
      (PUSHP ? '+' : '-'), (unsigned long)i);            \
  nsMemory::Free(s);                      \
  if (PUSHP                        \
    ? set->Add(i) < 0                  \
    : set->Remove(i) < 0)                \
  abort ();                      \
  if (!(NS_SUCCEEDED(set->Output(&s)))) abort ();          \
  printf ("%3lu: %-58s optimized =\n", (unsigned long)set->m_length, s);  \
  nsMemory::Free(s);                      \

#define END()                 \
  if (!(NS_SUCCEEDED(set->Output(&s)))) abort ();          \
  printf ("%3lu: %s\n\n", (unsigned long)set->m_length, s); \
  nsMemory::Free(s);                      \
  delete set;                 \



void
nsMsgKeySet::test_adder (void)
{
  const char *string;
  nsMsgKeySet *set;
  char *s;
  PRInt32 i;

  START("0-70,72-99,105,107,110-111,117-200");

  FROB(205, PR_TRUE);
  FROB(206, PR_TRUE);
  FROB(207, PR_TRUE);
  FROB(208, PR_TRUE);
  FROB(208, PR_TRUE);
  FROB(109, PR_TRUE);
  FROB(72, PR_TRUE);

  FROB(205, PR_FALSE);
  FROB(206, PR_FALSE);
  FROB(207, PR_FALSE);
  FROB(208, PR_FALSE);
  FROB(208, PR_FALSE);
  FROB(109, PR_FALSE);
  FROB(72, PR_FALSE);

  FROB(72, PR_TRUE);
  FROB(109, PR_TRUE);
  FROB(208, PR_TRUE);
  FROB(208, PR_TRUE);
  FROB(207, PR_TRUE);
  FROB(206, PR_TRUE);
  FROB(205, PR_TRUE);

  FROB(205, PR_FALSE);
  FROB(206, PR_FALSE);
  FROB(207, PR_FALSE);
  FROB(208, PR_FALSE);
  FROB(208, PR_FALSE);
  FROB(109, PR_FALSE);
  FROB(72, PR_FALSE);

  FROB(100, PR_TRUE);
  FROB(101, PR_TRUE);
  FROB(102, PR_TRUE);
  FROB(103, PR_TRUE);
  FROB(106, PR_TRUE);
  FROB(104, PR_TRUE);
  FROB(109, PR_TRUE);
  FROB(108, PR_TRUE);
  END();

  START("1-6"); FROB(7, PR_FALSE); END();
  START("1-6"); FROB(6, PR_FALSE); END();
  START("1-6"); FROB(5, PR_FALSE); END();
  START("1-6"); FROB(4, PR_FALSE); END();
  START("1-6"); FROB(3, PR_FALSE); END();
  START("1-6"); FROB(2, PR_FALSE); END();
  START("1-6"); FROB(1, PR_FALSE); END();
  START("1-6"); FROB(0, PR_FALSE); END();

  START("1-3"); FROB(1, PR_FALSE); END();
  START("1-3"); FROB(2, PR_FALSE); END();
  START("1-3"); FROB(3, PR_FALSE); END();

  START("1,3,5-7,9,10"); FROB(5, PR_FALSE); END();
  START("1,3,5-7,9,10"); FROB(6, PR_FALSE); END();
  START("1,3,5-7,9,10"); FROB(7, PR_FALSE); FROB(7, PR_TRUE); FROB(8, PR_TRUE);
  FROB (4, PR_TRUE); FROB (2, PR_FALSE); FROB (2, PR_TRUE);

  FROB (4, PR_FALSE); FROB (5, PR_FALSE); FROB (6, PR_FALSE); FROB (7, PR_FALSE);
  FROB (8, PR_FALSE); FROB (9, PR_FALSE); FROB (10, PR_FALSE); FROB (3, PR_FALSE);
  FROB (2, PR_FALSE); FROB (1, PR_FALSE); FROB (1, PR_FALSE); FROB (0, PR_FALSE);
  END();
}

#undef START
#undef FROB
#undef END



#define START(STRING) \
  string = STRING;    \
  if (!(set = nsMsgKeySet::Create(string))) abort ()

#define FROB(N,M)                        \
  i = N;                            \
  j = M;                            \
  if (!(NS_SUCCEEDED(set->Output(&s)))) abort ();          \
  printf ("%3lu: %-58s + %3lu-%3lu =\n", (unsigned long)set->m_length, s, (unsigned long)i, (unsigned long)j);  \
  nsMemory::Free(s);                      \
  switch (set->AddRange(i, j)) {                \
  case 0:                            \
  printf("(no-op)\n");                    \
  break;                            \
  case 1:                            \
  break;                            \
  default:                            \
  abort();                          \
  }                                \
  if (!(NS_SUCCEEDED(set->Output(&s)))) abort ();          \
  printf ("%3lu: %-58s\n", (unsigned long)set->m_length, s);            \
  nsMemory::Free(s);                      \


#define END()                 \
  if (!(NS_SUCCEEDED(set->Output(&s)))) abort ();          \
  printf ("%3lu: %s\n\n", (unsigned long)set->m_length, s); \
  nsMemory::Free(s);                      \
  delete set;


void
nsMsgKeySet::test_ranges(void)
{
  char *string;
  nsMsgKeySet *set;
  char *s;
  PRInt32 i;
  PRInt32 j;

  START("20-40,72-99,105,107,110-111,117-200");

  FROB(205, 208);
  FROB(50, 70);
  FROB(0, 10);
  FROB(112, 113);
  FROB(101, 101);
  FROB(5, 75);
  FROB(103, 109);
  FROB(2, 20);
  FROB(1, 9999);

  END();


#undef START
#undef FROB
#undef END
}




#define TEST(N)                    \
  if (! with_cache) set->m_cached_value = -1;    \
  if (!(NS_SUCCEEDED(set->Output(&s)))) abort ();          \
  printf (" %3d = %s\n", N,              \
      (set->IsMember(N) ? "true" : "false")); \
  nsMemory::Free(s);

void
nsMsgKeySet::test_member(bool with_cache)
{
  nsMsgKeySet *set;
  char *s;

  s = "1-70,72-99,105,107,110-111,117-200";
  printf ("\n\nTesting %s (with%s cache)\n", s, with_cache ? "" : "out");
  if (!(set = Create(s))) {
  abort ();
  }

  TEST(-1);
  TEST(0);
  TEST(1);
  TEST(20);
  
  delete set;
  s = "0-70,72-99,105,107,110-111,117-200";
  printf ("\n\nTesting %s (with%s cache)\n", s, with_cache ? "" : "out");
  if (!(set = Create(s))) {
  abort ();
  }
  
  TEST(-1);
  TEST(0);
  TEST(1);
  TEST(20);
  TEST(69);
  TEST(70);
  TEST(71);
  TEST(72);
  TEST(73);
  TEST(74);
  TEST(104);
  TEST(105);
  TEST(106);
  TEST(107);
  TEST(108);
  TEST(109);
  TEST(110);
  TEST(111);
  TEST(112);
  TEST(116);
  TEST(117);
  TEST(118);
  TEST(119);
  TEST(200);
  TEST(201);
  TEST(65535);

  delete set;
}

#undef TEST


// static void
// test_newsrc (char *file)
// {
//   FILE *fp = fopen (file, "r");
//   char buf [1024];
//   if (! fp) abort ();
//   while (fgets (buf, sizeof (buf), fp))
//   {
//     if (!strncmp (buf, "options ", 8))
//     fwrite (buf, 1, strlen (buf), stdout);
//     else
//     {
//       char *sep = buf;
//       while (*sep != 0 && *sep != ':' && *sep != '!')
//       sep++;
//       if (*sep) sep++;
//       while (isspace (*sep)) sep++;
//       fwrite (buf, 1, sep - buf, stdout);
//       if (*sep)
//       {
//         char *s;
//         msg_NewsRCSet *set = msg_parse_newsrc_set (sep, &allocinfo);
//         if (! set)
//         abort ();
//         if (! msg_OptimizeNewsRCSet (set))
//         abort ();
//         if (! ((s = msg_format_newsrc_set (set))))
//         abort ();
//         msg_free_newsrc_set (set, &allocinfo);
//         fwrite (s, 1, strlen (s), stdout);
//         free (s);
//         fwrite ("\n", 1, 1, stdout);
//       }
//     }
//   }
//   fclose (fp);
// }

void
nsMsgKeySet::RunTests ()
{

  test_decoder ("");
  test_decoder (" ");
  test_decoder ("0");
  test_decoder ("1");
  test_decoder ("123");
  test_decoder (" 123 ");
  test_decoder (" 123 4");
  test_decoder (" 1,2, 3, 4");
  test_decoder ("0-70,72-99,100,101");
  test_decoder (" 0-70 , 72 - 99 ,100,101 ");
  test_decoder ("0 - 268435455");
  /* This one overflows - we can't help it.
   test_decoder ("0 - 4294967295"); */

  test_adder ();

  test_ranges();

  test_member (PR_FALSE);
  test_member (PR_TRUE);

  // test_newsrc ("/u/montulli/.newsrc");
  /* test_newsrc ("/u/jwz/.newsrc");*/
}

#endif /* DEBUG */