NCL 204: Taking Strings Apart
In NCL 203, we used S.FIND to locate part of a string.
Consider:
EMMA:hello
Suppose : separates a name from some text.
We can find it:
#separator $000
#message "EMMA:hello"
S.FIND #separator #message ":"
D.TXT #separator
D.BLT
The program displays:
4
The separator is at position 4.
| Character | E |
M |
M |
A |
: |
h |
e |
l |
l |
o |
|---|---|---|---|---|---|---|---|---|---|---|
| Position | 0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
| From end | -10 |
-9 |
-8 |
-7 |
-6 |
-5 |
-4 |
-3 |
-2 |
-1 |
Knowing where something is lets us do more than test whether it exists.
We can use those positions to take the string apart.
Extracting part of a string
S.SUB extracts a range from a string:
S.SUB <dst> <str> <start> <end>
For example:
S.SUB $000 "Hello" 0 1
stores:
He
Both positions are included in the result.
So:
S.SUB $000 "Hello" 1 3
stores:
ell
We can use this to extract the name from our message:
#name $000
#message "EMMA:hello"
S.SUB #name #message 0 3
D.TXT #name
D.BLT
The program displays:
EMMA
Counting from the end
In NCL 203, we saw that positions can also be counted backward from the end of a string.
The final character is at position -1.
For example:
S.SUB $000 "Hello" -2 -1
stores:
lo
How negative positions are resolved
A negative position is resolved by adding it to the length of the string.
For example, "Hello!" has a length of 6. The position -1 resolves to:
6 + -1 = 5
So:
S.SUB $000 "Hello!" -1 -1
is effectively:
S.SUB $000 "Hello!" 5 5
and extracts:
!
Similarly, -2 resolves to position 4, -3 to position 3, and so on.
This is useful when we want a range to continue through the end of a string.
For our message:
EMMA:hello
the text begins at position 5.
We could write:
S.SUB $000 "EMMA:hello" 5 9
but that only works because this particular message ends at position 9.
Instead:
S.SUB $000 "EMMA:hello" 5 -1
means:
Start at position 5 and continue through the final character.
The result is:
hello
The same ending position works for a longer string:
S.SUB $000 "EMMA:Good morning!" 5 -1
which stores:
Good morning!
We don't need to know where the string ends.
Calculating a range
So far, we have still written the beginning and ending positions ourselves.
But S.FIND can give us the position of the separator while the program is running.
For:
EMMA:hello
we know:
#separator = 4
The name ends one position before the separator:
MOVE #leftEnd #separator
DEC #leftEnd
The text begins one position after it:
MOVE #rightStart #separator
INC #rightStart
This gives us:
#leftEnd = 3
#rightStart = 5
Those calculated positions can be used directly by S.SUB:
S.SUB #name #message 0 #leftEnd
S.SUB #text #message #rightStart -1
Putting that together:
#separator $000
#leftEnd $001
#rightStart $002
#name $003
#text $004
#message "EMMA:hello"
S.FIND #separator #message ":"
MOVE #leftEnd #separator
DEC #leftEnd
MOVE #rightStart #separator
INC #rightStart
S.SUB #name #message 0 #leftEnd
S.SUB #text #message #rightStart -1
D.TXT #name
D.TXT " / "
D.TXT #text
D.BLT
The program displays:
EMMA / hello
Now change:
#message "EMMA:hello"
to:
#message "ALICE:Good morning!"
The separator moves, but S.FIND finds its new position and the program calculates the new boundaries.
The result is:
ALICE / Good morning!
What happens at the edges?
Our format is supposed to look like:
NAME:text
But not every string containing : has text on both sides.
Consider:
:hello
The separator is at position 0, so our calculation gives:
#leftEnd = -1
#rightStart = 1
After the positions are resolved, the left range is reversed, so S.SUB produces an empty string.
The right side is extracted normally:
#name = ""
#text = "hello"
Now consider:
EMMA:
The separator is the final character. The name is extracted normally, while the requested right-hand range begins beyond the end of the string:
#name = "EMMA"
#text = ""
S.SUB can still perform the extraction. It is our NAME:text format that requires both extracted pieces to contain something.
There is one case we need to handle before extracting.
Only a separator
Consider:
:
Its only character is at position 0.
Our calculation for the end of the left side gives:
0 - 1 = -1
Normally, that gives us the position before the separator. But -1 is a negative position, so S.SUB resolves it from the end of the string.
":" has a length of 1:
1 + -1 = 0
So:
S.SUB #name ":" 0 -1
is effectively:
S.SUB #name ":" 0 0
and extracts:
:
Calculated negative positions still count from the end
Be careful when arithmetic produces a negative position.
S.SUB does not know that -1 was intended to mean “one position before 0.” It applies the normal position rules, where -1 means the final character of the string.
Check edge cases before extracting a range when your calculated boundaries might become negative.
This is the one case we need to reject before performing the split.
First, find the separator:
S.FIND #separator #message ":"
If it doesn't exist, there is nothing to split:
BEQ :invalid #separator -1
Then measure the original message:
S.LEN #length #message
If the separator exists and the entire message is only one character long, the message can only be ::
BEQ :invalid #length 1
For every other string containing a separator, we can perform the split and inspect the results.
Checking the extracted pieces
After splitting:
S.SUB #name #message 0 #leftEnd
S.SUB #text #message #rightStart -1
we want both pieces to contain at least one character.
We already know how to inspect the length of a string:
S.LEN #length #name
BEQ :invalid #length 0
S.LEN #length #text
BEQ :invalid #length 0
This rejects :hello because the extracted name has length 0, and EMMA: because the extracted text has length 0.
Notice the sequence we've built.
S.FIND gives us a numeric position. We perform arithmetic on that position to calculate two boundaries. S.SUB uses those boundaries to extract strings. Then S.LEN gives us numeric information about the results, which we check with the integer branches we already know.
We still haven't needed to compare the strings themselves.
Removing part of a string
Extracting ranges also gives us a way to remove something from the middle of a string.
Suppose we have:
Hello, ugly world!
and want:
Hello, world!
The part we want to remove occupies positions 7 through 11.
We can extract everything before it:
S.SUB #left #text 0 6
which gives:
Hello,
Then everything after it:
S.SUB #right #text 12 -1
which gives:
world!
Finally, join those pieces:
#left $000
#right $001
#text $002
S.MOVE #text "Hello, ugly world!"
S.SUB #left #text 0 6
S.SUB #right #text 12 -1
S.JOIN #text #left #right
D.TXT #text
D.BLT
The program displays:
Hello, world!
Removing a range is common enough to have its own instruction.
Deleting a range
S.DEL removes a range from a string:
S.DEL <dst> <str> <start> <end>
The positions work the same way as they do with S.SUB.
So the previous program can be reduced to:
#text $000
S.MOVE #text "Hello, ugly world!"
S.DEL #text #text 7 11
D.TXT #text
D.BLT
The result is still:
Hello, world!
Negative positions work here too.
For example:
S.DEL #text #text -1 -1
deletes the final character.
If:
#text = "Hello!"
the result is:
Hello
S.SUB keeps the selected range.
S.DEL removes the selected range.
Putting the splitter together
We now have everything needed to split and validate a message in the form:
NAME:text
Here is the complete program:
#separator $000
#length $001
#leftEnd $002
#rightStart $003
#name $004
#text $005
#message "EMMA:hello"
S.FIND #separator #message ":"
BEQ :invalid #separator -1
S.LEN #length #message
BEQ :invalid #length 1
MOVE #leftEnd #separator
DEC #leftEnd
MOVE #rightStart #separator
INC #rightStart
S.SUB #name #message 0 #leftEnd
S.SUB #text #message #rightStart -1
S.LEN #length #name
BEQ :invalid #length 0
S.LEN #length #text
BEQ :invalid #length 0
D.TXT "Name: "
D.TXT #name
D.TXT " / Text: "
D.TXT #text
JUMP :done
:invalid
D.TXT "Invalid message"
:done
D.BLT
With:
#message "EMMA:hello"
the program displays:
Name: EMMA / Text: hello
Try:
#message "ALICE:Good morning!"
The separator moves and both pieces change length, but the same program still extracts them correctly.
Then try:
:hello
EMMA:
hello
:
Each fails for a different reason.
Follow the program and determine which check sends each one to :invalid.
Try it
Start with the completed splitter and change the format from:
NAME:text
to a filename and extension:
filename.txt
Use . as the separator and extract:
filename
and:
txt
Try names of different lengths:
hello.ncl
STARTUP.ncl
notes.txt
The program should not depend on the separator appearing at any particular position.
Then try:
.hidden
filename.
Both strings contain the separator, so S.FIND succeeds.
Use the lengths of the extracted pieces to decide whether they are valid.
We can now find boundaries inside strings, calculate ranges from those positions, extract pieces, and inspect the results.
In the next lesson, NCL 205: Comparing and Interpreting Strings, we'll start making decisions based on what those extracted strings contain.