ccl.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* ccl - routines for character classes */
  2. /*-
  3. * Copyright (c) 1990 The Regents of the University of California.
  4. * All rights reserved.
  5. *
  6. * This code is derived from software contributed to Berkeley by
  7. * Vern Paxson.
  8. *
  9. * The United States Government has rights in this work pursuant
  10. * to contract no. DE-AC03-76SF00098 between the United States
  11. * Department of Energy and the University of California.
  12. *
  13. * Redistribution and use in source and binary forms are permitted provided
  14. * that: (1) source distributions retain this entire copyright notice and
  15. * comment, and (2) distributions including binaries display the following
  16. * acknowledgement: ``This product includes software developed by the
  17. * University of California, Berkeley and its contributors'' in the
  18. * documentation or other materials provided with the distribution and in
  19. * all advertising materials mentioning features or use of this software.
  20. * Neither the name of the University nor the names of its contributors may
  21. * be used to endorse or promote products derived from this software without
  22. * specific prior written permission.
  23. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  24. * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  25. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  26. */
  27. #ifndef lint
  28. static char rcsid[] =
  29. "@(#) $Id$ (LBL)";
  30. #endif
  31. #include "flexdef.h"
  32. /* ccladd - add a single character to a ccl
  33. *
  34. * synopsis
  35. * int cclp;
  36. * int ch;
  37. * ccladd( cclp, ch );
  38. */
  39. void ccladd( cclp, ch )
  40. int cclp;
  41. int ch;
  42. {
  43. int ind, len, newpos, i;
  44. len = ccllen[cclp];
  45. ind = cclmap[cclp];
  46. /* check to see if the character is already in the ccl */
  47. for ( i = 0; i < len; ++i )
  48. if ( ccltbl[ind + i] == ch )
  49. return;
  50. newpos = ind + len;
  51. if ( newpos >= current_max_ccl_tbl_size )
  52. {
  53. current_max_ccl_tbl_size += MAX_CCL_TBL_SIZE_INCREMENT;
  54. ++num_reallocs;
  55. ccltbl = reallocate_character_array( ccltbl, current_max_ccl_tbl_size );
  56. }
  57. ccllen[cclp] = len + 1;
  58. ccltbl[newpos] = ch;
  59. }
  60. /* cclinit - make an empty ccl
  61. *
  62. * synopsis
  63. * int cclinit();
  64. * new_ccl = cclinit();
  65. */
  66. int cclinit()
  67. {
  68. if ( ++lastccl >= current_maxccls )
  69. {
  70. current_maxccls += MAX_CCLS_INCREMENT;
  71. ++num_reallocs;
  72. cclmap = reallocate_integer_array( cclmap, current_maxccls );
  73. ccllen = reallocate_integer_array( ccllen, current_maxccls );
  74. cclng = reallocate_integer_array( cclng, current_maxccls );
  75. }
  76. if ( lastccl == 1 )
  77. /* we're making the first ccl */
  78. cclmap[lastccl] = 0;
  79. else
  80. /* the new pointer is just past the end of the last ccl. Since
  81. * the cclmap points to the \first/ character of a ccl, adding the
  82. * length of the ccl to the cclmap pointer will produce a cursor
  83. * to the first free space
  84. */
  85. cclmap[lastccl] = cclmap[lastccl - 1] + ccllen[lastccl - 1];
  86. ccllen[lastccl] = 0;
  87. cclng[lastccl] = 0; /* ccl's start out life un-negated */
  88. return ( lastccl );
  89. }
  90. /* cclnegate - negate a ccl
  91. *
  92. * synopsis
  93. * int cclp;
  94. * cclnegate( ccl );
  95. */
  96. void cclnegate( cclp )
  97. int cclp;
  98. {
  99. cclng[cclp] = 1;
  100. }
  101. /* list_character_set - list the members of a set of characters in CCL form
  102. *
  103. * synopsis
  104. * int cset[CSIZE];
  105. * FILE *file;
  106. * list_character_set( cset );
  107. *
  108. * writes to the given file a character-class representation of those
  109. * characters present in the given set. A character is present if it
  110. * has a non-zero value in the set array.
  111. */
  112. void list_character_set( file, cset )
  113. FILE *file;
  114. int cset[];
  115. {
  116. register int i;
  117. char *readable_form();
  118. putc( '[', file );
  119. for ( i = 0; i < csize; ++i )
  120. {
  121. if ( cset[i] )
  122. {
  123. register int start_char = i;
  124. putc( ' ', file );
  125. fputs( readable_form( i ), file );
  126. while ( ++i < csize && cset[i] )
  127. ;
  128. if ( i - 1 > start_char )
  129. /* this was a run */
  130. fprintf( file, "-%s", readable_form( i - 1 ) );
  131. putc( ' ', file );
  132. }
  133. }
  134. putc( ']', file );
  135. }