Code Explainers

Code explainers tagged #parsing

ruby
class UserAgentParser
  BROWSERS = [
    [/Edg\/([\d.]+)/, "Edge"],
    [/OPR\/([\d.]+)/, "Opera"],

Parsing user-agent strings in Ruby

regex pattern-matching lookup-tables
Intermediate 8 steps
javascript
function evaluate(expression) {
  const tokens = tokenize(expression);
  let pos = 0;
 

Building a recursive descent calculator

parsing recursion operator-precedence
Intermediate 8 steps
rust
use chrono::{Duration, NaiveDate};
 
#[derive(Debug)]
pub struct DateRange {

Parsing and iterating date ranges in Rust

error-handling iterators parsing
Intermediate 7 steps
java
public final class ImportOrganizer {
 
    private static final Pattern IMPORT_LINE =
            Pattern.compile("^import\\s+(static\\s+)?([\\w.]+(?:\\.\\*)?)\\s*;\\s*$");

Sorting Java imports with a regex pass

regex sorting text-processing
Intermediate 8 steps
rust
pub fn extract_quoted_fields(line: &str) -> Vec<String> {
    let mut fields = Vec::new();
    let mut current = String::new();
    let mut in_quotes = false;

Parsing quoted fields with a state machine in Rust

state-machine parsing string-processing
Intermediate 7 steps
typescript
interface ContentRange {
  unit: string;
  start: number;
  end: number;

Parsing HTTP Content-Range headers in TypeScript

regex parsing validation
Intermediate 8 steps
ruby
require 'set'
 
class CronSchedule
  FIELDS = %i[minute hour day month wday].freeze

Parsing and matching cron expressions in Ruby

parsing time-arithmetic sets
Intermediate 10 steps
javascript
const UNIT_MULTIPLIERS = {
  ms: 1,
  s: 1000,
};

Parsing CSS durations into milliseconds

parsing regular-expressions validation
Intermediate 6 steps
java
public final class IniParser {
 
    private static final Pattern SECTION = Pattern.compile("\\[(.+?)\\]");
 

Parsing INI files in Java

parsing regex state-machine
Intermediate 10 steps
rust
use std::collections::BTreeSet;
 
#[derive(Debug)]
pub enum FieldError {

Parsing a cron field in Rust

parsing error-handling enums
Intermediate 7 steps
typescript
interface ParsedCookie {
  name: string;
  value: string;
  domain?: string;

Parsing a Set-Cookie header in TypeScript

parsing string-manipulation type-safety
Intermediate 9 steps
ruby
require "base64"
 
module Auth
  module BasicCredentials

Parsing HTTP Basic Auth headers in Ruby

parsing authentication base64
Intermediate 6 steps
java
public final class UserAgentParser {
 
    private static final Pattern OS_PATTERN = Pattern.compile(
            "(Windows NT [0-9.]+|Mac OS X [0-9_]+|Android [0-9.]+|iPhone OS [0-9_]+|Linux)");

Parsing user-agent strings with regex in Java

regex parsing pattern-matching
Intermediate 9 steps
ruby
module Dotenv
  LINE = /\A\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*?)\s*\z/
 
  module_function

Parsing a .env file in Ruby

parsing regular-expressions environment-variables
Intermediate 8 steps
go
package config
 
import (
	"bufio"

Parsing INI files in Go

parsing state-machine error-handling
Intermediate 7 steps
typescript
type RGB = { r: number; g: number; b: number };
 
function parseHex(hex: string): RGB {
  const normalized = hex.replace(/^#/, "").trim();

Lightening and darkening hex colors in TypeScript

bitwise color-manipulation parsing
Intermediate 9 steps
php
<?php
 
namespace App\Http;
 

HTTP content negotiation in PHP

http content-negotiation parsing
Intermediate 9 steps
python
import re
from dataclasses import dataclass, field
 
 

Building a table of contents from Markdown

regular-expressions parsing slugification
Intermediate 9 steps