/src/lib/string/internal/fillable_string.e

http://github.com/tybor/Liberty · Specman e · 119 lines · 97 code · 12 blank · 10 comment · 4 complexity · f6e6dac9905acf4da1d58a9fd3b09869 MD5 · raw file

  1. class FILLABLE_STRING
  2. -- A string of text made of fixed text and placeholdes
  3. obsolete "[
  4. That was a tentative implementation.
  5. The idea was to save the explicit allocation required by STRING_PRINTER which is used like
  6. my_string_printer.put_message
  7. ("@(1) is really a nice @(2)",
  8. <<"Eiffel", "programming language">>)
  9. Too bad that the idea behind FILLABLE_STRING is to scan the message
  10. during creation of Current creating a list of abstract strings
  11. (`pieces') holding the fixed pieces and the variable one.
  12. The variable ones would FILLABLE_ARGUMENTs, a heir of ABSTRACT_STRING
  13. implemented with a REFERENCE[LAZY_STRING]. The indexes arguments would
  14. be stored into array `placeholders_indexes'.
  15. So in the end I would save an explicit array allocation requiring a
  16. linked list and an array. Smart, isn't it?
  17. Actually current implementation of arg, creating a new rope each time
  18. arg is invoked requires N allocation of a small object.
  19. ]"
  20. inherit ABSTRACT_STRING
  21. creation make
  22. feature
  23. make (a_string: ABSTRACT_STRING; an_index: INTEGER; a_value: ABSTRACT_STRING) is
  24. do
  25. create pieces.make
  26. create placeholders.make
  27. initialize_with(a_string)
  28. -- the following is plainly wrong but you got the idea
  29. -- if placeholders.has(an_index) then
  30. -- placeholders.put(an_index,a_value)
  31. -- end
  32. end
  33. arg (an_index: INTEGER; a_value: ABSTRACT_STRING): like Current is
  34. do
  35. placeholdes.put(an_index,a_value)
  36. not_yet_implemented
  37. -- je tutto sbagliato, ye tutto da rifare.
  38. end
  39. feature {} -- Implementation
  40. initialize_with (a_string: ABSTRACT_STRING) is
  41. -- Initialize Current from `a_string'
  42. require a_string/=Void
  43. local
  44. ch: CHARACTER -- or better like first or item
  45. state: INTEGER
  46. in_progress: STRING
  47. placeholder: FILLABLE_STRING_PLACEHOLDER
  48. i, backtrack_i, index: INTEGER
  49. do
  50. -- TODO: evaluate an eventual reimplementation using
  51. -- iterators. This implementation is taken from
  52. -- MESSAGE_FORMATTER
  53. create in_progress.make_empty
  54. from i:=a_string.lower; state:=normal_state
  55. until i>a_string.upper
  56. loop
  57. from
  58. until i>a_string.upper
  59. loop
  60. ch := a_string.item(i)
  61. inspect state
  62. when print_state then
  63. in_progress.append_character(ch)
  64. state := normal_state
  65. when normal_state then
  66. if ch.is_equal(delimiter) then
  67. backtrack_i := i - 1
  68. state := after_delimeter_state
  69. else in_progress.append_character(ch)
  70. end
  71. when after_delimeter_state then
  72. if ch.is_equal(delimiter) then
  73. in_progress.append_character(ch)
  74. state := normal_state
  75. elseif ch.is_equal(opening_brace) then
  76. index := 0
  77. state := after_opening_brace_state
  78. else
  79. i := backtrack_i
  80. state := print_state
  81. end
  82. when after_opening_brace_state then
  83. if ch.is_decimal_digit then
  84. index := 10*index + ch.decimal_value
  85. elseif ch.is_equal(closing_brace) then
  86. pieces.add_last(in_progress)
  87. create in_progress.make_empty
  88. create placeholder
  89. placeholders.put(index,placeholder)
  90. pieces.add_last(placeholder)
  91. state := normal_state
  92. else
  93. i := backtrack_i
  94. state := print_state
  95. end
  96. end
  97. i:=i+1
  98. end
  99. end
  100. feature {} -- Implementation
  101. pieces: LINKED_LIST[ABSTRACT_STRING]
  102. placeholders_indexes: ARRAY[INTEGER]
  103. print_state, normal_state, after_delimeter_state, after_opening_brace_state: INTEGER is unique
  104. delimiter: CHARACTER is '#'
  105. opening_brace: CHARACTER is '('
  106. closing_brace: CHARACTER is ')'
  107. end