Loading ...
Sorry, an error occurred while loading the content.

Re: [eiffel-nice-library] STRING: index_of, substring_index

Expand Messages
  • Peter Horan
    ... I was thinking perhaps index_of should not have a precondition of valid_index, but just return the off the end result. YMMV. ... This is the additional
    Message 1 of 9 , Jul 26, 2000
    • 0 Attachment
      "Durchholz, Joachim" wrote:
      >
      > Peter,
      >
      > would you please mind answering my question? I really don't understand, and
      > your answer is confusing me (first you refer to example code written by me,
      > then to an example by you, I don't know whether you mean code in STRING or
      > in STRING's clients, etc. etc.) - sorting this out is going to take me more
      > time than you.

      This is the code:
      > Here is the code fragment (Other features are s: STRING; x: ARRAY[STRING]; separator: CHARACTER):
      > local
      > field, field_start, separator_position: INTEGER
      > do
      > from
      > field := 1
      > field_start := 1
      > until
      > separator_position > s.count or else
      > field > max_tests
      > loop
      > -- if statement to protect index_of
      > -- (should be unnecessary)
      I was thinking perhaps index_of should not have a precondition of valid_index, but just return the off the end result. YMMV.
      > if field_start <= s.count
      > then
      > separator_position :=
      > s.index_of(separator, field_start)
      > else
      > separator_position := s.count + 1
      > end
      >
      This is the additional code I had to put in because I wanted the continuous
      result. It would have been unnecessary if count + 1 had been returned:
      > -- Handle the absent separator case
      > if separator_position = 0
      > then
      > separator_position := s.count + 1
      > end
      >
      > -- Do the work
      > x.item(field) :=
      > s.substring(field_start, separator_position - 1)
      >
      > field := field + 1
      > field_start := separator_position + 1
      > end
      > end

      I wrote this code and revisited it, twice, before I properly understood
      what was required. It is the old fencepost problem, of course. If I refactor
      the code, I might write in the body
      if has_from(s, field_start, separator)
      then
      separator_position := s.index_of(separator, field-start)
      else
      separator_position := s.count + 1
      end
      x.item_field := s.substring(field_start, separator_position - 1)

      where
      has_from(s: STRING; field_start, separator: INTEGER) is
      -- Does s contain the separator at or after the field_start?
      do
      Result := field_start <= s.count and then
      s.index_of(separator, field_start) /= 0
      end

      Hmm... Refactoring, if this is right, improves the code. My requirement for
      count + 1 is a little weaker.

      But my concern was also having the right features to guide my thinking in the
      first place. "has_from" may have given me a nudge.
      --
      Peter Horan School of Computing and Mathematics
      peter@... Deakin University
      +61-3-5227 1234 (Voice) Geelong, Victoria 3217, AUSTRALIA
      +61-3-5227 2028 (FAX) http://www.cm.deakin.edu.au/~peter

      -- The Eiffel guarantee: From specification to implementation
      -- (http://www.cetus-links.org/oo_eiffel.html)
    Your message has been successfully submitted and would be delivered to recipients shortly.