Module brutils_license_plate

Utilities for Brazilian vehicle license plates.

Description

Utilities for Brazilian vehicle license plates.

Two plate patterns exist: the old format — 3 letters followed by 4 digits ("ABC1234") — and the Mercosul format — 3 letters, a digit, a letter and 2 digits ("ABC1D23"). Validation accepts lowercase input; formatting and conversion emit uppercase.

All functions operate on UTF-8 binaries.

Data Types

formatted_plate()

formatted_plate() = <<_:56>> | <<_:64>>

A display-formatted plate: Mercosul plates are bare (<<"ABC1D23">>), old-format plates carry a dash (<<"ABC-1234">>).

plate()

plate() = <<_:56>>

A raw plate: 7 characters in either pattern, e.g. <<"ABC1D23">>.

plate_type()

plate_type() = old_format | mercosul

The two plate patterns: old_format is LLLNNNN, mercosul is LLLNLNN.

Function Index

convert_to_mercosul/1Converts an old-format plate (LLLNNNN) to the Mercosul pattern (LLLNLNN) by replacing the digit at position 5 with a letter: 0 becomes A, 1 becomes B, ...
format/1Formats a valid license plate for display: old-format plates get a dash after the letters (<<"ABC-1234">>), Mercosul plates come out bare — both uppercased.
generate/0Generates a random valid Mercosul plate.
generate/1Generates a random valid plate in the given pattern: <<"LLLNNNN">> (old format) or <<"LLLNLNN">> (Mercosul), case insensitive.
get_format/1Detects the pattern of a license plate: {ok, old_format} for LLLNNNN, {ok, mercosul} for LLLNLNN, or {error, invalid} when the input matches neither.
is_valid/1Returns whether the given term is a valid license plate of either pattern, old format or Mercosul.
is_valid/2Returns whether the given term is a valid license plate of the given pattern: old_format (LLLNNNN) or mercosul (LLLNLNN).
remove_symbols/1Removes the dash (-) from a license plate string.

Function Details

convert_to_mercosul/1

convert_to_mercosul(Plate::binary()) -> {ok, plate()} | {error, invalid}

Converts an old-format plate (LLLNNNN) to the Mercosul pattern (LLLNLNN) by replacing the digit at position 5 with a letter: 0 becomes A, 1 becomes B, ... 9 becomes J.

The input is normalized (trimmed, uppercased) first and must be a valid old-format plate — an already-Mercosul plate yields {error, invalid}, not a no-op. (For whitespace-padded input the reference implementation converts the wrong position and keeps the padding, an artifact of slicing the unstripped string; this port deliberately normalizes first and converts correctly.)

  1> brutils_license_plate:convert_to_mercosul(<<"abc4567">>).
  {ok,<<"ABC4F67">>}
  2> brutils_license_plate:convert_to_mercosul(<<"ABC1D23">>).
  {error,invalid}

format/1

format(Plate::binary()) -> {ok, formatted_plate()} | {error, invalid}

Formats a valid license plate for display: old-format plates get a dash after the letters (<<"ABC-1234">>), Mercosul plates come out bare — both uppercased.

The input is normalized (trimmed, uppercased) first. (For whitespace-padded input the reference implementation emits a mangled result — dash misplaced, padding kept — an artifact of slicing the unstripped string; this port deliberately normalizes first.)

  1> brutils_license_plate:format(<<"abc1234">>).
  {ok,<<"ABC-1234">>}
  2> brutils_license_plate:format(<<"abc1e34">>).
  {ok,<<"ABC1E34">>}

generate/0

generate() -> {ok, plate()}

Generates a random valid Mercosul plate.

Equivalent to generate(<<"LLLNLNN">>), but with no error case — the default pattern is always valid.

  1> brutils_license_plate:generate().
  {ok,<<"BAR9B69">>}

generate/1

generate(Pattern::binary()) -> {ok, plate()} | {error, invalid}

Generates a random valid plate in the given pattern: <<"LLLNNNN">> (old format) or <<"LLLNLNN">> (Mercosul), case insensitive. Each L becomes a uniform uppercase letter and each N a uniform digit.

  1> brutils_license_plate:generate(<<"LLLNNNN">>).
  {ok,<<"KCM5068">>}
  2> brutils_license_plate:generate(<<"XXXXXXX">>).
  {error,invalid}

get_format/1

get_format(Plate::binary()) -> {ok, plate_type()} | {error, invalid}

Detects the pattern of a license plate: {ok, old_format} for LLLNNNN, {ok, mercosul} for LLLNLNN, or {error, invalid} when the input matches neither.

Trimming and case rules are those of is_valid/1. The result composes with is_valid/2: a detected type always validates the plate it was detected from. (The reference implementation returns the pattern strings "LLLNNNN" / "LLLNLNN" instead — this port deliberately returns the plate_type() atoms.)

  1> brutils_license_plate:get_format(<<"abc1234">>).
  {ok,old_format}
  2> brutils_license_plate:get_format(<<"ABC1D23">>).
  {ok,mercosul}

is_valid/1

is_valid(Plate::term()) -> boolean()

Returns whether the given term is a valid license plate of either pattern, old format or Mercosul.

Surrounding ASCII whitespace is trimmed and letter case is ignored, so <<" abc1234 ">> is valid — but embedded symbols such as the display dash are not stripped. Existence is never verified. The function is total: any non-binary term returns false rather than raising.

  1> brutils_license_plate:is_valid(<<"ABC1234">>).
  true
  2> brutils_license_plate:is_valid(<<"abc1d23">>).
  true

is_valid/2

is_valid(Plate::term(), X2::plate_type()) -> boolean()

Returns whether the given term is a valid license plate of the given pattern: old_format (LLLNNNN) or mercosul (LLLNLNN).

Trimming and case rules are those of is_valid/1. The type must be one of the two atoms; anything else is out of contract and raises.

  1> brutils_license_plate:is_valid(<<"ABC1234">>, old_format).
  true
  2> brutils_license_plate:is_valid(<<"ABC1234">>, mercosul).
  false

remove_symbols/1

remove_symbols(Plate::binary()) -> binary()

Removes the dash (-) from a license plate string.

Only the dash is removed — the narrowest cleaning rule in this library: dots, spaces and letter case are all kept unchanged. This is a pure character filter: it does not validate the input.

  1> brutils_license_plate:remove_symbols(<<"ABC-123">>).
  <<"ABC123">>
  2> brutils_license_plate:remove_symbols(<<"abc.12 3">>).
  <<"abc.12 3">>


Generated by EDoc