Code Explainers

Code explainers tagged #parsing

rust
use std::net::Ipv4Addr;
use std::str::FromStr;
 
#[derive(Debug, Clone, Copy)]

Parsing and matching IPv4 CIDR ranges in Rust

bitwise-operations parsing error-handling
Intermediate 8 steps
java
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Map;
 

Evaluating math expressions with two stacks

stacks parsing operator-precedence
Intermediate 9 steps
python
def is_valid_card_number(number: str) -> bool:
    digits = [int(c) for c in number if c.isdigit()]
 
    if len(digits) < 13 or len(digits) > 19:

Validating card numbers with the Luhn check

checksum validation luhn-algorithm
Intermediate 6 steps
ruby
require "yaml"
 
class FrontMatter
  DELIMITER = /\A---\s*\n(.*?)\n---\s*\n?(.*)\z/m

Parsing YAML front matter in Ruby

parsing regular-expressions yaml
Intermediate 8 steps
javascript
const FRONTMATTER_RE = /^---\r?\n([\s\S]*?)\r?\n---\r?\n?/;
 
function coerce(value) {
  const trimmed = value.trim();

Parsing YAML-style frontmatter in JavaScript

parsing regular-expressions type-coercion
Intermediate 9 steps
ruby
module FixedWidthParser
  FIELDS = [
    { name: :record_type, range: 0...2 },
    { name: :account_id,  range: 2...12, type: :integer },

Parsing fixed-width records in Ruby

parsing data-driven-design type-coercion
Intermediate 5 steps
java
public final class MultipartParser {
 
    public record FilePart(String name, String filename, String contentType, byte[] content) {}
 

Parsing multipart form data by hand in Java

parsing byte-arrays http
Advanced 8 steps
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