200-Level Challenge: Chat Log
We've spent the 200-level learning how to store, inspect, search, split, modify, and construct strings.
Now it's time to put those pieces together.
Your challenge is to turn a structured string containing a conversation into a formatted chat log.
The Conversation
Your program will receive the entire conversation as a single string:
#chat "Alice:Knock knock!|Bob:Who's there?|Alice:Tank|Bob:Tank who?|Alice:You're welcome!"
The format is:
sender:message|sender:message|sender:message
A pipe character (|) separates one message from the next.
Within each message, the first colon (:) separates the sender's name from the contents of the message.
Your program should turn the conversation into something like:
CHAT LOG
------------------------
ALICE: Knock knock!
BOB: Who's there?
ALICE: Tank
BOB: Tank who?
ALICE: You're welcome!
------------------------
The sender names should be displayed in colour, while the message contents should use normal white text.
The Rules
Your program should:
- Process the conversation from beginning to end.
- Work with any number of messages, rather than assuming there are exactly five.
- Extract the sender and contents of each message.
- Convert sender names to uppercase.
- Give each sender a colour determined from the first character of their name.
- Restore normal white text before displaying the message contents.
- Preserve the contents of each message exactly.
- Construct the completed chat log in memory before sending it to the Display.
You may assume that every message contains a sender, a colon, and some message contents.
You may also assume that the pipe character appears only between messages.
Choosing a Colour
Don't assign colours to particular names yourself.
Instead, calculate a colour from the sender's name.
Use the Unicode codepoint of the first character:
colour = ORD(first character) % 16
The result will be a palette entry from 0 through 15.
NCS/e's normal inline Display attributes occupy:
U+F100–U+F10F
so the corresponding attribute codepoint is:
style = 0xF100 + colour
Convert that codepoint back into a character and place it immediately before the sender's name.
For example, the first character of ALICE is A.
Its codepoint is 65:
65 % 16 = 1
so Alice's style character is:
U+F101
Bob begins with B, giving him:
U+F102
Your program shouldn't need to know what colours those palette entries contain. It only needs to calculate the appropriate Display attribute.
Test Your Parser
Once the knock-knock conversation works, try this conversation:
#chat "Charlie:Hey David, what time is it?|David:It's 12:30.|Charlie:Thanks!"
The result should contain:
CHAT LOG
------------------------
CHARLIE: Hey David, what time is it?
DAVID: It's 12:30.
CHARLIE: Thanks!
------------------------
Charlie and David should automatically receive colours based on their names.
Pay particular attention to:
David:It's 12:30.
There are two colons in that message.
Only the first separates the sender from the contents.
The second belongs to the message itself, so the final result must still contain:
12:30
If it doesn't, have another look at how you're separating the sender from the message.
Hints
Try solving the challenge yourself before opening these.
How do I process one message at a time?
Start with the entire conversation in a working memory location.
Find the first |.
You need two results:
- everything before the pipe is the next message;
- everything after the pipe is the conversation that remains.
You've already learned an operation that can produce either result by deleting the half you don't want.
Still stuck?
Find the next pipe:
S.FIND #pipe #remaining "|"
Then use S.DEL twice:
S.DEL #message #remaining #pipe -1
S.DEL #remaining #remaining 0 #pipe
The first instruction deletes the pipe and everything after it, leaving only the message:
Alice:Knock knock!
The second deletes the message and the pipe, leaving the rest of the conversation:
Bob:Who's there?|Alice:Tank|...
This lets you split around a delimiter without calculating the positions immediately before and after it.
What about the final message?
Eventually there won't be another |.
Remember what S.FIND returns when it cannot find the requested string.
At that point, everything still in #remaining is the final message.
Still stuck?
Check for -1 after searching:
S.FIND #pipe #remaining "|"
BEQ :last #pipe -1
At :last, the entire remaining string is the message:
:last
S.MOVE #message #remaining
S.MOVE #remaining ""
Clearing #remaining also gives the program a convenient way to know that there are no more messages afterward.
How do I separate the sender from the contents?
Once you have one complete message, find its first colon.
This is another delimiter, so you can use the same S.DEL technique again.
Delete the colon and everything after it to get the sender.
Delete everything through the colon to get the contents.
Still stuck?
Find the colon:
S.FIND #colon #message ":"
Then split the message around it:
S.DEL #sender #message #colon -1
S.DEL #contents #message 0 #colon
Given:
David:It's 12:30.
this produces:
#sender = "David"
#contents = "It's 12:30."
S.FIND found the first colon. The second colon was never used as a delimiter, so it remains part of #contents.
How do I generate the sender's colour?
First convert the sender's name to uppercase.
Then read the Unicode codepoint of its first character.
Follow the formula from the challenge:
colour = ORD(first character) % 16
style = 0xF100 + colour
Finally, turn the calculated codepoint back into a character.
Still stuck?
Convert the name first:
S.UPR #sender #sender
Then calculate the Display attribute:
S.ORD #colour #sender 0
MOD #colour #colour 16
ADD #colour #colour 0xF100
S.CHAR #style #colour
#style now contains the actual PUA character for that Display attribute.
It can be joined into another string just like any other character.
How do I construct each line?
Each line consists of several pieces:
style + sender + white + ": " + contents + line break
The sender's style was calculated while the program was running, so you can't write its \sXX escape into the source.
The return to white is always the same, so you can.
Still stuck?
Append each piece to #output:
S.JOIN #output #output #style
S.JOIN #output #output #sender
S.JOIN #output #output "\s0F: "
S.JOIN #output #output #contents
S.JOIN #output #output "\r\n"
#style contains the dynamically generated PUA character.
\s0F produces the fixed U+F10F attribute that restores normal white text before the colon and message contents.
How do I repeat for every message?
After formatting one message, look at #remaining.
If it still contains something, another message is waiting to be processed.
If it's empty, the final message has already been processed and the loop is finished.
Still stuck?
Branch back to the beginning while #remaining isn't empty:
BSNEQ :next #remaining ""
The :last path clears #remaining, so the same test works for both ordinary and final messages.
Show me one solution
There are several ways to arrange the program.
Here's one:
-- Conversation
#chat "Alice:Knock knock!|Bob:Who's there?|Alice:Tank|Bob:Tank who?|Alice:You're welcome!"
-- Working memory
#remaining $000
#message $001
#sender $002
#contents $003
#style $004
#output $005
#separator $006
#pipe $007
#colon $008
#colour $009
-- Clear the Display by scrolling
#clear "\s0F\f\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \f"
-- Prepare the output
S.MOVE #remaining #chat
S.PADR #separator "" "-" 24
S.MOVE #output "\f\s06CHAT LOG\s0F\r\n"
S.JOIN #output #output #separator
S.JOIN #output #output "\r\n"
:next
-- Extract the next message
S.FIND #pipe #remaining "|"
BEQ :last #pipe -1
S.DEL #message #remaining #pipe -1
S.DEL #remaining #remaining 0 #pipe
JUMP :parse
:last
-- No pipe remains, so this is the final message
S.MOVE #message #remaining
S.MOVE #remaining ""
:parse
-- Separate sender from contents
S.FIND #colon #message ":"
S.DEL #sender #message #colon -1
S.DEL #contents #message 0 #colon
-- Format the sender
S.UPR #sender #sender
S.ORD #colour #sender 0
MOD #colour #colour 16
ADD #colour #colour 0xF100
S.CHAR #style #colour
-- Add the message to the chat log
S.JOIN #output #output #style
S.JOIN #output #output #sender
S.JOIN #output #output "\s0F: "
S.JOIN #output #output #contents
S.JOIN #output #output "\r\n"
-- Continue if another message remains
BSNEQ :next #remaining ""
-- Finish the log
S.JOIN #output #output #separator
-- Display it
D.TXT #clear
D.TXT #output
D.BLT
Trace the First Message
At the beginning:
#remaining = "Alice:Knock knock!|Bob:Who's there?|..."
S.FIND locates the first pipe.
We then use the same position twice:
S.DEL #message #remaining #pipe -1
S.DEL #remaining #remaining 0 #pipe
The first deletion removes the pipe and everything after it:
#message = "Alice:Knock knock!"
The second removes everything through the pipe:
#remaining = "Bob:Who's there?|Alice:Tank|..."
We can use exactly the same technique with the colon:
S.FIND #colon #message ":"
S.DEL #sender #message #colon -1
S.DEL #contents #message 0 #colon
giving:
#sender = "Alice"
#contents = "Knock knock!"
After changing the sender to uppercase:
#sender = "ALICE"
S.ORD gives us the codepoint of A:
65
The colour calculation gives:
65 % 16 = 1
Adding the beginning of the Display attribute range gives:
0xF100 + 1 = 0xF101
S.CHAR turns that codepoint into the actual U+F101 character.
The program appends that character, the sender, a return to normal white, and the message contents to #output.
Execution then returns to :next with the shortened conversation in #remaining.
The same process continues until no pipe remains.
Try the Second Conversation
Now replace the original conversation with:
#chat "Charlie:Hey David, what time is it?|David:It's 12:30.|Charlie:Thanks!"
Don't change anything else.
Your program should still process the complete conversation.
Charlie begins with C:
67 % 16 = 3
so his name uses U+F103.
David begins with D:
68 % 16 = 4
so his name uses U+F104.
More importantly, the program should display:
DAVID: It's 12:30.
unchanged.
The colon in 12:30 never causes a problem because the program only searches for the first colon in each complete message. Once that boundary has been found, everything after it belongs to the contents.
The same program has now handled different senders, different numbers of messages, different message lengths, dynamically generated Display attributes, and punctuation that happens to resemble part of the input format.
That's the end of the 200-level.
We've gone from storing our first string to searching, splitting, comparing, modifying, formatting, and constructing text. We've also seen that strings can carry more than visible letters: Unicode characters, control characters, and Display attributes can all be manipulated as data.
In this challenge, we combined those ideas into a small program that turns structured data into something meant for a person to read.
Ready to continue? In NCL 301: Reading the Keyboard, we'll stop providing all of our strings in advance and start receiving them from the person using the computer.