- Text consists of a sequence of newline delimited lines
- EOF may also end a non-empty line.
- Within a line, position i is associated with the character at
index i.
- Position $ last character plus one.
- Make one pass through the file, editing each line in turn.
- Editing commands are relative to the text line most recently read from
input.
- This is the current line.
- Text editing commands.
- length - Print the size of the current line, in characters.
- position str- set the position to the start of str.
- insert str - insert str at the position.
- delete n - delete n characters at the position.
- replace str1 str2 - replace the leftmost instance of
str1 in the current line by str2.
- next - output the current line, read the next line.
- quit - copy the rest of input to output and exit.
- This is (sorta) known as stream editing.
- Data structures.
- Input and output - files and streams.
- The edit buffer - a single string.
- Positions, commands - ints, strings, chars.
- The algorithm.
- Prompt for input and output files.
- Repeatedly prompt for commands.
- Clean up and exit.
- Most of the editor commands can be implemented in terms of string
operators.
- length - use
.size()
.
- position str -
position = line.find(str)
.
- insert str -
line.insert(position, str)
.
- delete n -
line.erase(position, n)
.
- replace str1 str2 -
line(line.replace(position,
trr1.length(), str2)
.
- next -
out << line << "\n" ; in >> line
.
- A simple stream-oriented, line-oriented text editor.
- Stream editing's great for batch work.
- Doesn't mix well with interaction.
- There's no pieces we can use elsewhere.
- You can imagine some program that might want to edit some text.
- C-C++ are notoriously poor string-handling languages.
- You can imagine some application wanting a text-editing component.
- A mail program, for example.
- Can we do better?
- Text consists of a sequence of newline delimited lines
- EOF may also end a non-empty line.
- A position i appears to the left of the first character in the
ith line.
- Or where the first character would be if the line's empty
- The first line is line 0.
- Position $ is the start of the nth line in an n-line file.
- Text to be edited lives in the text buffer.
- The edit cursor is always located at some edit buffer position.
- Initially it's at position $.
- Editing commands are relative to the edit cursor.
- The current line is the line following the edit cursor.
- Text editing commands.
- input filename - replace the contents of the edit buffer with
the contents of the named file.
- output filename - write the contents of the edit buffer to the
file with the given name.
- clearbuffer - delete the contents of the edit buffer.
- insert str - insert str as a new line before the current
line.
- move i - move the edit cursor currently at position p to
position p + i. If p + i is less than zero, move to
position 0; if p + i is greater than n, move to position $.
- search str - search forward from current line for the first line
containing str; move the edit cursor to that line if found.
- replace str1 str2 - replace the leftmost instance of
str1 in the current line by str2. Do nothing if str1
doesn't occur in the current line.
- We could do the same as Nyhoff: one big program.
- No pieces we can easily reuse.
- The operations are more complicated now, the data structures need to
be up to it to simplify our task.
- This problem would be easy if we had an text-edit ADT that did all the
commands for us.
- *Poof* here it is: the edit-buffer ADT. Now what?
- Adapt the Nyhoff's command-interpreter part to use an edit-buffer
instance.
- What does the edit-buffer ADT do?
- To figure out what it does, figure out how it's used.
- Text editing commands.
- input filename - read the file into the edit buffer.
- output filename - write the edit buffer into a file.
- clearbuffer - delete the contents of the edit buffer.
- insert str - insert a line into the edit buffer.
- move i - move the edit cursor.
- search str - find a line containing a string.
- replace str1 str2 - replace one string with another.
- That looks like a lot of work? Can the interface be simplified?
- Is input and output really necessary?
- Most programmers know how to do input and output.
- Also, suppose the text isn't in a file.
- On the other hand,
i
- and ostreams
are general mechanisms.
- Is clearing the buffer really necessary?
- Throw away the old buffer and get a new one.
- On the other hand, reusing the buffer may be more efficient.
- And probably more complicated to implement too.
- This is an inteface question, leave it out with an option to add
later.
- If you add it, the option to remove it later is much more
difficult.
- The remaining commands are relatively independent of one another.
- So much for the outside. What's the edit buffer look like on the
inside?
- Having built our walls, we can be as dumb as we want inside.
- Now about one big string?
- You need to balance dumbness and lazyness.
- One big string is dumb, but finding lines will take a lot of work.
- As Einstein might have said, "You should be as dumb as possible,
but no dumber."
- One big string is just too dumb.
- If finding lines is the problem, how about an array of lines?
- Better - dumb and serviceable.
- It's dumb because arrays are static, and the number of lines of text
is dynamic.
This page last modified on 11 October 2003.