ladybird/Servers/TelnetServer/Parser.cpp
Andreas Kling 6f4c380d95 AK: Use size_t for the length of strings
Using int was a mistake. This patch changes String, StringImpl,
StringView and StringBuilder to use size_t instead of int for lengths.
Obviously a lot of code needs to change as a result of this.
2019-12-09 17:51:21 +01:00

63 lines
1.6 KiB
C++

#include <AK/Function.h>
#include <AK/String.h>
#include <AK/Types.h>
#include "Parser.h"
void Parser::write(const StringView& data)
{
for (size_t i = 0; i < data.length(); i++) {
u8 ch = data[i];
switch (m_state) {
case State::Free:
switch (ch) {
case IAC:
m_state = State::ReadCommand;
break;
case '\r':
if (on_data)
on_data("\n");
break;
default:
if (on_data)
on_data(StringView(&ch, 1));
break;
}
break;
case State::ReadCommand:
switch (ch) {
case IAC: {
m_state = State::Free;
if (on_data)
on_data("\xff");
break;
}
case CMD_WILL:
case CMD_WONT:
case CMD_DO:
case CMD_DONT:
m_command = ch;
m_state = State::ReadSubcommand;
break;
default:
m_state = State::Error;
if (on_error)
on_error();
break;
}
break;
case State::ReadSubcommand: {
auto command = m_command;
m_command = 0;
m_state = State::Free;
if (on_command)
on_command({ command, ch });
break;
}
case State::Error:
// ignore everything
break;
}
}
}