Skip to content

v.util #

Copyright (c) 2025 Alexander Medvednikov. All rights reserved. Use of this source code is governed by an MIT license that can be found in the LICENSE file.

Constants #

const external_module_dependencies_for_tool = {
	'vdoc': ['markdown']
}

external_module_dependencies_for_tool lists the modules from outside vlib that a bundled tool needs before it can be compiled. v build-tools installs these up front, and the v launcher installs them before it compiles a tool on demand, so that building the tools does not fail on a fresh checkout.

const project_boundary_markers = ['.git', '.hg', '.svn', pref.module_search_stop_marker]

project_boundary_markers are the entries that mark a directory as the root of a checkout or, with pref.module_search_stop_marker, as an explicit end of the module search. The v.mod lookup stops here, so an unrelated project above the boundary is never mistaken for the one being compiled. Only the explicit marker also ends the search for modules in the directories above it (see pref.is_module_search_stop_dir): a checkout marker does not, because a project may be built against a module checked out next to it.

fn args_quote_paths #

fn args_quote_paths(args []string) string

args_quote_paths quotes paths and joins them into a shell command fragment.

fn arm64_asm_register_names #

fn arm64_asm_register_names() []string

arm64_asm_register_names returns the register names an arm64 assembly block can use.

fn asm_clobber_is_special #

fn asm_clobber_is_special(name string) bool

asm_clobber_is_special reports whether name is one of the GNU pseudo clobbers that never name a register.

fn asm_register_names #

fn asm_register_names(arch string) []string

asm_register_names returns every register name an assembly block for arch can use. An empty result means the architecture has no table here, so callers must not treat unknown names as errors.

fn check_module_is_installed #

fn check_module_is_installed(modulename string, is_verbose bool, need_update bool) !bool

check_module_is_installed makes sure that modulename is present in ~/.vmodules, cloning it from https://github.com/vlang/ when it is not. With need_update set, an already installed module is refreshed with v update. A failed update is only a warning: updates fail on transient network problems, and the copy that is already there usually still works.

fn closest_asm_register #

fn closest_asm_register(name string, registers []string) ?string

closest_asm_register returns the register in registers that name was most likely meant to be, or none when nothing is close enough to be worth suggesting.

fn contains_capital #

fn contains_capital(s string) bool

contains_capital reports whether s contains an uppercase character.

fn ensure_modules_for_all_tools_are_installed #

fn ensure_modules_for_all_tools_are_installed(is_verbose bool)

ensure_modules_for_all_tools_are_installed installs every module named in external_module_dependencies_for_tool. It is called by v build-tools before it starts compiling, so a missing dependency is reported once, up front, instead of as a confusing "unknown module" error from the middle of a tool's build.

fn ensure_modules_for_tool_are_installed #

fn ensure_modules_for_tool_are_installed(tool_name string, tool_source string, is_verbose bool) !

ensure_modules_for_tool_are_installed installs the modules from outside vlib that the bundled tool tool_name, with its sources in tool_source, needs before it is compiled without a -path. It errs on the side of leaving a module alone: a folder named like the module, with .v files or a v.mod in it, in any VMODULES root, or in the folder of tool_source or a folder above it (like the tool's project root), counts as the module. If that folder is not a usable module after all, the compiler reports it, just like without this check. So a module is installed only when it is clearly missing, and this does not touch the network in the common case, and works offline. An empty tool_source skips the folder lookup. The returned error names the module that could not be installed, and how to install it manually, instead of leaving the user with a cannot import module builder error.

fn external_modules_for_tool #

fn external_modules_for_tool(tool_name string) []string

external_modules_for_tool returns the modules from outside vlib that the bundled tool tool_name (for example vdoc) needs before it can be compiled.

fn find_all_v_files #

fn find_all_v_files(roots []string) ![]string

find_all_v_files resolves V files below the supplied files and directories.

fn githash #

fn githash(path string) !string

githash returns the current seven-character Git commit hash for path.

fn is_escape_sequence #

@[inline]
fn is_escape_sequence(c u8) bool

is_escape_sequence reports whether c is a valid escape sequence denoter.

fn is_project_boundary_dir #

fn is_project_boundary_dir(dir string) bool

is_project_boundary_dir reports whether dir carries one of the project_boundary_markers (a .git file counts too: git worktrees use one).

fn nearest_vmod_root #

fn nearest_vmod_root(path string) ?string

nearest_vmod_root returns the closest directory containing a v.mod for path.

fn new_suggestion #

fn new_suggestion(wanted string, possibilities []string, params SuggestionParams) Suggestion

new_suggestion creates a diagnostic suggestion from wanted and possibilities.

fn parse_inline_asm_header #

fn parse_inline_asm_header(source string) InlineAsmHeader

parse_inline_asm_header reads the instruction set and template modifiers from the text preceding an assembly block's opening brace.

fn path_styled_for_error_messages #

fn path_styled_for_error_messages(path string) string

path_styled_for_error_messages returns a stable path for diagnostics.

fn quote_path #

fn quote_path(path string) string

quote_path quotes a path for use in a shell command.

fn resolve_env_value #

fn resolve_env_value(str string, check_for_presence bool) !string

resolve_env_value replaces all occurrences of $env('ENV_VAR_NAME') in str with the value of the env variable $ENV_VAR_NAME.

fn set_vroot_folder #

fn set_vroot_folder(vroot_path string)

set_vroot_folder exposes the V executable and marks child tool processes.

fn tokenize_c_flag #

fn tokenize_c_flag(value string) []string

tokenize_c_flag splits a C flag on unquoted whitespace while preserving quotes.

fn verror #

@[noreturn]
fn verror(kind string, message string)

verror prints a tool error and exits.

fn x86_asm_register_names #

fn x86_asm_register_names() []string

x86_asm_register_names mirrors the i386 and amd64 register set V's assembly blocks accept, including the numbered general purpose, vector, mask, control and debug ones.

type CalculateSuggestionSimilarityFN #

type CalculateSuggestionSimilarityFN = fn (s1 string, s2 string) f32

CalculateSuggestionSimilarityFN compares two suggestion candidates.

fn (Suggestion) say #

fn (s Suggestion) say(message string) string

say appends the best suggestion, or a short possibility list, to message.

struct InlineAsmHeader #

struct InlineAsmHeader {
pub:
	arch        string
	is_goto     bool
	is_volatile bool
	is_raw      bool
	is_intel    bool
}

InlineAsmHeader describes the words a V assembly block writes before its {, for example asm amd64 raw intel {.

struct KeyRecentCache #

@[heap]
struct KeyRecentCache {
mut:
	// 128 slots * 160 bytes of key storage.
	keys   [20480]u8
	a_lens [key_recent_slots]int
	b_lens [key_recent_slots]int
	c_lens [key_recent_slots]int
	values [key_recent_slots]string
	states [key_recent_slots]i8 // 1 = found, -1 = known miss, 0 = empty
}

KeyRecentCache is a lossy front cache for lookups keyed by up to three strings. It keeps its own copy of every key, so callers may pass temporary strings, and a hit compares the parts directly: the caller skips building the composite key its backing map is keyed by. Values are returned as stored. Only use it in front of maps that are not cleared while the cache is alive.

fn (KeyRecentCache) get #

@[direct_array_access]
fn (c &KeyRecentCache) get(a string, b string, cc string) (i8, string)

get returns 1 and the cached value for a hit, -1 for a cached miss, and 0 when the key is not cached.

fn (KeyRecentCache) put #

@[direct_array_access]
fn (mut c KeyRecentCache) put(a string, b string, cc string, state i8, value string)

put records state (1 = found, -1 = known miss) and value for the key.

struct SuggestionParams #

@[params]
struct SuggestionParams {
pub mut:
	similarity_threshold f32 = 0.5
	similarity_fn        CalculateSuggestionSimilarityFN = strings.dice_coefficient
}

SuggestionParams configures new_suggestion.