increment internal tile values by one so strings can be null-terminated

This commit is contained in:
ehouse 2007-02-14 15:17:00 +00:00
parent f273e93c86
commit 3b7e680f2c

View file

@ -755,13 +755,13 @@ writeOutStartNode( const char* startNodeOut, int firstRootChildOffset )
static void static void
makeTableHash( void ) makeTableHash( void )
{ {
int i; int ii;
FILE* TABLEFILE = fopen( gTableFile, "r" ); FILE* TABLEFILE = fopen( gTableFile, "r" );
if ( NULL == TABLEFILE ) { if ( NULL == TABLEFILE ) {
ERROR_EXIT( "unable to open %s\n", gTableFile ); ERROR_EXIT( "unable to open %s\n", gTableFile );
} }
for ( i = 0; ; ++i ) { for ( ii = 0; ; ++ii ) {
int ch = getc(TABLEFILE); int ch = getc(TABLEFILE);
if ( ch == EOF ) { if ( ch == EOF ) {
break; break;
@ -777,17 +777,20 @@ makeTableHash( void )
gRevMap.push_back(ch); gRevMap.push_back(ch);
if ( ch == 0 ) { // blank if ( ch == 0 ) { // blank
gBlankIndex = i; gBlankIndex = ii;
// we want to increment i when blank seen since it is a // we want to increment i when blank seen since it is a
// tile value // tile value
continue; continue;
} }
// die "$0: $gTableFile too large\n" // die "$0: $gTableFile too large\n"
assert( i < 64 ); assert( ii < 64 );
// die "$0: only blank (0) can be 64th char\n" ; // die "$0: only blank (0) can be 64th char\n" ;
assert( i < 64 || ch == 0 ); assert( ii < 64 || ch == 0 );
gTableHash[ch] = i; // Add 1 to i so no tile-strings contain 0 and we can treat as
// null-terminated. The 1 is subtracted again in
// outputNode().
gTableHash[ch] = ii + 1;
} }
fclose( TABLEFILE ); fclose( TABLEFILE );
@ -958,7 +961,8 @@ outputNode( Node node, int nBytes, FILE* outfile )
ERROR_EXIT( "fco not 1 or 0" ); ERROR_EXIT( "fco not 1 or 0" );
} }
unsigned char chIn5 = TrieNodeGetLetter(node); // - 1 below reverses + 1 in makeTableHash()
unsigned char chIn5 = TrieNodeGetLetter(node) - 1;
unsigned char bits = chIn5; unsigned char bits = chIn5;
if ( bits > 0x1F && nBytes == 3 ) { if ( bits > 0x1F && nBytes == 3 ) {
ERROR_EXIT( "char %d too big", bits ); ERROR_EXIT( "char %d too big", bits );