Code Explainers

Code explainers tagged #parsing

javascript
const IBAN_LENGTHS = {
  DE: 22, FR: 27, GB: 22, ES: 24, IT: 27,
  NL: 18, BE: 16, CH: 21, AT: 20, PT: 25,
};

How IBAN validation works in JavaScript

validation checksum modular-arithmetic
Intermediate 8 steps
rust
use std::time::Duration;
 
#[derive(Debug, PartialEq)]
pub enum ParseDurationError {

Parsing duration strings safely in Rust

parsing error-handling checked-arithmetic
Intermediate 8 steps
go
package logparse
 
import (
	"bufio"

Splitting multi-line logs with a Scanner

parsing streaming bufio
Intermediate 9 steps
javascript
function parseHexColor(hex) {
  const cleaned = hex.trim().replace(/^#/, '');
 
  const expand = (short) =>

Parsing hex colors into RGBA channels

parsing bitwise regex
Intermediate 7 steps
rust
use std::fs::File;
use std::io::{Read, Seek, SeekFrom};
 
#[derive(Debug)]

Parsing an HTTP Range header in Rust

http-range parsing file-io
Intermediate 10 steps
typescript
interface UserAgentInfo {
  browser: { name: string; version: string };
  os: { name: string; version: string };
  device: 'mobile' | 'tablet' | 'desktop';

Parsing a user-agent string with ordered rules

regex parsing pattern-matching
Intermediate 9 steps
typescript
interface ParsedName {
  first: string;
  middle: string;
  last: string;

Parsing human names into structured parts

parsing string-manipulation normalization
Intermediate 9 steps
python
import datetime
from dataclasses import dataclass
 
 

Parsing fixed-width records in Python

parsing generators dataclasses
Intermediate 8 steps
rust
use std::collections::HashMap;
 
#[derive(Debug)]
pub struct RequestHead {

Parsing an HTTP request head in Rust

parsing error-handling iterators
Intermediate 9 steps
php
<?php
 
final class MarkdownParser
{

Building a small Markdown-to-HTML parser in PHP

state-machine parsing closures
Intermediate 10 steps
go
package humanize
 
import (
	"fmt"

Parsing human-readable byte sizes in Go

parsing regex lookup-table
Intermediate 8 steps
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