Code Explainers

Code explainers tagged #parsing

python
from configparser import ConfigParser, ExtendedInterpolation
from pathlib import Path
 
 

Layered INI config loading in Python

configuration parsing defaults
Intermediate 8 steps
python
from urllib.parse import urlparse
 
 
class RobotsRules:

Parsing and applying a robots.txt file

parsing longest-prefix-match state-machine
Intermediate 10 steps
rust
use once_cell::sync::Lazy;
use regex::Regex;
 
static LOG_LINE: Lazy<Regex> = Lazy::new(|| {

Parsing access logs with a lazy regex in Rust

regex lazy-initialization parsing
Intermediate 7 steps
typescript
type Semver = {
  major: number;
  minor: number;
  patch: number;

Parsing and comparing semver strings in TypeScript

parsing regular-expressions comparison
Intermediate 9 steps
typescript
type NestedValue = string | NestedValue[] | { [key: string]: NestedValue };
 
function parseFieldPath(name: string): string[] {
  const match = name.match(/^([^\[\]]+)((?:\[[^\[\]]*\])*)$/);

Parsing bracketed form field names into nested objects

parsing recursive-types regex
Intermediate 8 steps
rust
#[derive(Debug, PartialEq)]
enum State {
    FieldStart,
    InUnquoted,

Parsing a CSV line with a state machine

state machine parsing enums
Intermediate 9 steps
php
final class Route
{
    private string $regex;
    private array $paramNames = [];

Compiling route patterns into regex in PHP

routing regex parsing
Intermediate 8 steps
java
package com.example.time;
 
import java.time.Duration;
import java.time.Instant;

Adding ISO-8601 durations across time zones in Java

date-time iso-8601 parsing
Intermediate 7 steps
java
public record ServerConfig(
        String host,
        int port,
        Duration timeout,

Loading typed config into a Java record

records factory-methods parsing
Intermediate 7 steps
go
package tsvio
 
import (
	"bufio"

Reading and writing TSV records in Go

parsing io-streams error-handling
Intermediate 10 steps
ruby
class QueryParser
  def self.parse(query_string)
    new(query_string).parse
  end

Parsing nested query strings in Ruby

parsing recursion tokenization
Intermediate 9 steps
rust
use std::cmp::Ordering;
use std::str::FromStr;
 
#[derive(Debug, Clone, PartialEq, Eq)]

Parsing and ordering semantic versions in Rust

parsing trait-implementation ordering
Intermediate 8 steps
ruby
class ApacheLogParser
  LINE_PATTERN = /\A(?<ip>\S+)\s\S+\s\S+\s\[(?<time>[^\]]+)\]\s"(?<method>[A-Z]+)\s(?<path>\S+)\s(?<protocol>[^"]+)"\s(?<status>\d{3})\s(?<bytes>\d+|-)/
 
  TIME_FORMAT = "%d/%b/%Y:%H:%M:%S %z"

Parsing Apache logs with named captures

regex named-captures parsing
Intermediate 6 steps
java
public record FixedWidthField(String name, int start, int end) {
 
    public String extract(String line) {
        int from = Math.min(start, line.length());

Parsing fixed-width text in Java

records parsing streams
Intermediate 7 steps
ruby
require "csv"
 
def parse_transaction_row(line)
  fields = CSV.parse_line(line, headers: false, skip_blanks: true)

Parsing one CSV transaction row in Ruby

parsing type-coercion error-handling
Intermediate 6 steps
rust
use std::collections::HashMap;
use std::fs::File;
use std::io::{self, BufRead, BufReader};
use std::path::Path;

Parsing INI files in Rust

parsing file-io hashmap
Intermediate 9 steps
typescript
type CsvRecord = Record<string, string>;
 
function parseCsv(input: string): CsvRecord[] {
  const rows: string[][] = [];

Parsing CSV with a character state machine

state-machine parsing string-processing
Intermediate 8 steps
typescript
const ISO_DURATION = /^P(?:(\d+(?:\.\d+)?)Y)?(?:(\d+(?:\.\d+)?)M)?(?:(\d+(?:\.\d+)?)W)?(?:(\d+(?:\.\d+)?)D)?(?:T(?:(\d+(?:\.\d+)?)H)?(?:(\d+(?:\.\d+)?)M)?(?:(\d+(?:\.\d+)?)S)?)?$/;
 
const SECONDS_PER = {
  years: 365 * 24 * 60 * 60,

Parsing ISO 8601 durations to seconds

regex parsing iso-8601
Intermediate 7 steps