0162

The Comma That Broke the Import

Algorithm
Medium
text
parsing
csv

The Comma That Broke the Import

Monday's customer import from the webshop died on line 4,217: the buyer "Davis, Sam" turned into two columns, shifting every field after the name one position to the right — the city landed in the phone number. The export is perfectly valid CSV: fields containing commas are wrapped in double quotes, and a literal quote inside such a field is written as two quotes. The import code just splits on commas. Business Central integrations meet this format constantly — you are building the line parser that gets it right.

Requirements

Create a codeunit named "CSV Line Parser" with one public procedure:

procedure ParseLine(Line: Text): List of [Text]

Parsing rules — all seven are graded:

  1. Fields are separated by commas; the returned list contains the field values in their original order.
  2. A field may be enclosed in double quotes; the enclosing quotes are not part of the value.
  3. Inside a quoted field, a comma is ordinary data, not a separator.
  4. Inside a quoted field, a doubled quote "" stands for one literal " in the value.
  5. Everything else comes back exactly as written — spaces are never trimmed.
  6. Empty fields are real fields: first,,last has three fields, ,mid, has three fields, "" is an empty field, and the empty line parses to exactly one field — the empty text.
  7. A line whose quoted field is still open when the line ends is malformed: raise an error with a message that contains the text unterminated quoted field — this exact fragment; note the casing.

What you may rely on — the tests never violate this: apart from the malformed case in rule 7, every quote character in a test line either opens a field (at the start of the line or right after a separator comma), closes a quoted field, or is part of a doubled pair inside a quoted field. You will never see a quote in the middle of an unquoted field, or data between a closing quote and the next comma.

What the tests check

Fixed cases for every rule above — plain splits, kept empty fields, preserved spaces, commas inside quotes, doubled quotes (including a field whose entire value is one " character), adjacent quoted fields, the empty line, and the unterminated-quote error — plus one line assembled from randomized field values containing commas and quotes, so hardcoding the examples fails. All comparisons are exact, character for character.

Learn More

Hint 1
Splitting on every comma is exactly the bug from the incident: a comma inside quotes is data, not a separator. At every character you need to know one thing — are you inside a quoted field right now?
Hint 2
Walk the line one character at a time with a Boolean in-quotes flag: outside quotes, a quote turns the flag on and a comma finishes the current field; everything else joins the field being built. Remember to add the field you are building when the line ends — the empty line still yields its one empty field — and if the flag is still on at that point, that is the unterminated-quote error.
Hint 3
The escape is the only two-character token: inside a quoted field, when you meet a quote, look at the NEXT character. Another quote means append one literal quote and skip past both; anything else (or the end of the line) means the quoted section just closed.
ALBusiness Central 28.4
Press Compile to check your code compiles — Submit runs the tests.
The code editor is desktop-only
Open this problem on a computer to write and run code. Reading the description, tests and discussion works fine here.