Code Explainers
Code explainers tagged #parsing
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
go
package parser import ( "fmt"
Parsing access logs with named regex groups in Go
regex
named-groups
parsing
Intermediate
5 steps
java
public final class AccessLogParser { private static final Pattern LINE = Pattern.compile( "^(?<ip>\\S+) \\S+ \\S+ \\[(?<time>[^\\]]+)\\] "
Parsing Apache access logs in Java
regex
named-groups
parsing
Intermediate
7 steps
rust
use std::io::{BufRead, BufReader}; use std::process::{Command, Stdio}; #[derive(Debug)]
Parsing ps output into structs in Rust
subprocess
parsing
iterators
Intermediate
8 steps
ruby
class DurationParser UNIT_SECONDS = { 'w' => 604_800, 'd' => 86_400,
Parsing duration strings into seconds in Ruby
parsing
regular-expressions
validation
Intermediate
7 steps
javascript
function parseJwtPayload(token) { const parts = token.split('.'); if (parts.length !== 3) { throw new Error('Invalid JWT: expected 3 segments');
Decoding a JWT payload in JavaScript
jwt
base64url
encoding
Intermediate
6 steps
ruby
class SemanticVersion include Comparable attr_reader :major, :minor, :patch, :prerelease
Comparable semantic versions in Ruby
comparable
parsing
operator-overloading
Intermediate
8 steps
php
<?php namespace App\Config;
Writing a .env file loader in PHP
parsing
environment-variables
configuration
Intermediate
9 steps
typescript
type JsonValue = Record<string, unknown> | unknown[]; export function parseJsonStream<T = JsonValue>( stream: ReadableStream<Uint8Array>,
Streaming JSON parsing with a depth counter
streams
parsing
state-machine
Advanced
9 steps
rust
use std::num::ParseIntError; fn parse_line(line: &str) -> Result<Vec<i64>, ParseIntError> { line.split(',')
Collecting Results in Rust iterators
iterators
error-handling
result
Intermediate
7 steps
ruby
require "time" require "tzinfo" module TimeConverter
Converting timestamps across time zones with TZInfo
timezones
iso8601
parsing
Intermediate
8 steps
rust
use std::collections::HashMap; use std::fmt; #[derive(Debug)]
Parsing a config file with typed errors in Rust
error-handling
enums
parsing
Intermediate
9 steps