Code Explainers

Code explainers tagged #string-processing

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