Code Explainers

Code explainers tagged #string-processing

ruby
def wrap_text(text, width = 80)
  raise ArgumentError, "width must be positive" unless width.positive?
 
  text.split(/\n/, -1).map do |paragraph|

How greedy text wrapping works in Ruby

string-processing greedy-algorithm text-wrapping
Intermediate 7 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
go
package payment
 
import (
	"regexp"

Masking card numbers in Go

regex string-processing redaction
Intermediate 6 steps
go
package slug
 
import (
	"regexp"

Turning titles into URL slugs in Go

unicode-normalization regexp string-processing
Intermediate 8 steps
rust
pub fn normalize_path(input: &str) -> String {
    let is_absolute = input.starts_with('/');
    let has_trailing_slash = input.len() > 1 && input.ends_with('/');
    let mut stack: Vec<&str> = Vec::new();

Normalizing filesystem paths in Rust

string-processing stack path-manipulation
Intermediate 8 steps
rust
use once_cell::sync::Lazy;
use regex::Regex;
 
static NON_ALPHANUMERIC: Lazy<Regex> = Lazy::new(|| Regex::new(r"[^a-z0-9]+").unwrap());

Building URL slugs in Rust

string-processing regex transliteration
Intermediate 8 steps
java
public final class EmailNormalizer {
 
    private static final Pattern EMAIL_PATTERN = Pattern.compile(
        "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$"

Normalizing email addresses in Java

validation regex normalization
Intermediate 8 steps
rust
use std::collections::BTreeSet;
use std::fs::File;
use std::io::{self, BufRead, BufReader, BufWriter, Write};
use std::path::Path;

Deduplicating and sorting words in Rust

file-io btreeset string-processing
Intermediate 6 steps
rust
pub fn collapse_whitespace(input: &str) -> String {
    let mut result = String::with_capacity(input.len());
    let mut in_whitespace = false;
 

Collapsing runs of whitespace in Rust

string-processing state-machine iteration
Beginner 5 steps
javascript
function escapeHtml(str) {
  return str.replace(/[&<>"']/g, (ch) => ({
    '&': '&amp;',
    '<': '&lt;',

Safely highlighting search matches in text

html-escaping regex search-highlighting
Intermediate 7 steps
java
public final class Slugifier {
 
    private static final Pattern NON_LATIN = Pattern.compile("[^\\w-]");
    private static final Pattern WHITESPACE = Pattern.compile("[\\s]+");

Building a URL slugifier in Java

regex unicode-normalization string-processing
Intermediate 8 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
python
import re
import unicodedata
 
_SLUG_STRIP = re.compile(r"[^\w\s-]")

Building URL-safe slugs in Python

string-processing regex unicode-normalization
Intermediate 7 steps
rust
use std::borrow::Cow;
 
fn sanitize_filename(input: &str) -> Cow<'_, str> {
    if input.chars().all(|c| c.is_alphanumeric() || matches!(c, '.' | '-' | '_')) {

Avoiding allocations with Cow in Rust

clone-on-write borrowing zero-copy
Intermediate 7 steps
php
<?php
 
namespace App\Support;
 

Building a URL slug from any title in PHP

regex transliteration string-processing
Intermediate 8 steps