/vendor/gc/include/private/cord_pos.h

http://github.com/feyeleanor/RubyGoLightly · C++ Header · 118 lines · 47 code · 22 blank · 49 comment · 4 complexity · 56abb788c2eb93713c1ae682c6ec9516 MD5 · raw file

  1. /*
  2. * Copyright (c) 1993-1994 by Xerox Corporation. All rights reserved.
  3. *
  4. * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
  5. * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
  6. *
  7. * Permission is hereby granted to use or copy this program
  8. * for any purpose, provided the above notices are retained on all copies.
  9. * Permission to modify the code and to distribute modified code is granted,
  10. * provided the above notices are retained, and a notice that the code was
  11. * modified is included with the above copyright notice.
  12. */
  13. /* Boehm, May 19, 1994 2:23 pm PDT */
  14. # ifndef CORD_POSITION_H
  15. /* The representation of CORD_position. This is private to the */
  16. /* implementation, but the size is known to clients. Also */
  17. /* the implementation of some exported macros relies on it. */
  18. /* Don't use anything defined here and not in cord.h. */
  19. # define MAX_DEPTH 48
  20. /* The maximum depth of a balanced cord + 1. */
  21. /* We don't let cords get deeper than MAX_DEPTH. */
  22. struct CORD_pe {
  23. CORD pe_cord;
  24. size_t pe_start_pos;
  25. };
  26. /* A structure describing an entry on the path from the root */
  27. /* to current position. */
  28. typedef struct CORD_Pos {
  29. size_t cur_pos;
  30. int path_len;
  31. # define CORD_POS_INVALID (0x55555555)
  32. /* path_len == INVALID <==> position invalid */
  33. const char *cur_leaf; /* Current leaf, if it is a string. */
  34. /* If the current leaf is a function, */
  35. /* then this may point to function_buf */
  36. /* containing the next few characters. */
  37. /* Always points to a valid string */
  38. /* containing the current character */
  39. /* unless cur_end is 0. */
  40. size_t cur_start; /* Start position of cur_leaf */
  41. size_t cur_end; /* Ending position of cur_leaf */
  42. /* 0 if cur_leaf is invalid. */
  43. struct CORD_pe path[MAX_DEPTH + 1];
  44. /* path[path_len] is the leaf corresponding to cur_pos */
  45. /* path[0].pe_cord is the cord we point to. */
  46. # define FUNCTION_BUF_SZ 8
  47. char function_buf[FUNCTION_BUF_SZ]; /* Space for next few chars */
  48. /* from function node. */
  49. } CORD_pos[1];
  50. /* Extract the cord from a position: */
  51. CORD CORD_pos_to_cord(CORD_pos p);
  52. /* Extract the current index from a position: */
  53. size_t CORD_pos_to_index(CORD_pos p);
  54. /* Fetch the character located at the given position: */
  55. char CORD_pos_fetch(CORD_pos p);
  56. /* Initialize the position to refer to the give cord and index. */
  57. /* Note that this is the most expensive function on positions: */
  58. void CORD_set_pos(CORD_pos p, CORD x, size_t i);
  59. /* Advance the position to the next character. */
  60. /* P must be initialized and valid. */
  61. /* Invalidates p if past end: */
  62. void CORD_next(CORD_pos p);
  63. /* Move the position to the preceding character. */
  64. /* P must be initialized and valid. */
  65. /* Invalidates p if past beginning: */
  66. void CORD_prev(CORD_pos p);
  67. /* Is the position valid, i.e. inside the cord? */
  68. int CORD_pos_valid(CORD_pos p);
  69. char CORD__pos_fetch(CORD_pos);
  70. void CORD__next(CORD_pos);
  71. void CORD__prev(CORD_pos);
  72. #define CORD_pos_fetch(p) \
  73. (((p)[0].cur_end != 0)? \
  74. (p)[0].cur_leaf[(p)[0].cur_pos - (p)[0].cur_start] \
  75. : CORD__pos_fetch(p))
  76. #define CORD_next(p) \
  77. (((p)[0].cur_pos + 1 < (p)[0].cur_end)? \
  78. (p)[0].cur_pos++ \
  79. : (CORD__next(p), 0))
  80. #define CORD_prev(p) \
  81. (((p)[0].cur_end != 0 && (p)[0].cur_pos > (p)[0].cur_start)? \
  82. (p)[0].cur_pos-- \
  83. : (CORD__prev(p), 0))
  84. #define CORD_pos_to_index(p) ((p)[0].cur_pos)
  85. #define CORD_pos_to_cord(p) ((p)[0].path[0].pe_cord)
  86. #define CORD_pos_valid(p) ((p)[0].path_len != CORD_POS_INVALID)
  87. /* Some grubby stuff for performance-critical friends: */
  88. #define CORD_pos_chars_left(p) ((long)((p)[0].cur_end) - (long)((p)[0].cur_pos))
  89. /* Number of characters in cache. <= 0 ==> none */
  90. #define CORD_pos_advance(p,n) ((p)[0].cur_pos += (n) - 1, CORD_next(p))
  91. /* Advance position by n characters */
  92. /* 0 < n < CORD_pos_chars_left(p) */
  93. #define CORD_pos_cur_char_addr(p) \
  94. (p)[0].cur_leaf + ((p)[0].cur_pos - (p)[0].cur_start)
  95. /* address of current character in cache. */
  96. #endif