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 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/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 comptime_string_bound #
fn comptime_string_bound(raw string) ?int
comptime_string_bound parses a literal slice bound without truncating it to int.
fn comptime_string_index #
fn comptime_string_index(receiver string, needle string) (ComptimeStringScalar, bool)
comptime_string_index folds s.index(needle). The bool reports whether the needle was found, which is the none half of the option the checker typed.
fn comptime_string_repeat #
fn comptime_string_repeat(receiver string, count_text string) ?ComptimeStringScalar
comptime_string_repeat folds s.repeat(count) for compile-time-known operands. repeat is the one string operation in the wasm stub gap whose argument is an int, so it cannot travel through comptime_string_scalar's string-only arguments.
fn comptime_string_scalar #
fn comptime_string_scalar(receiver string, method string, args []string) ?ComptimeStringScalar
comptime_string_scalar calls the builtin implementation on compile-time-known operands.
fn comptime_string_split #
fn comptime_string_split(receiver string, delim string) []string
comptime_string_split folds s.split(delim) for compile-time-known operands.
fn contains_capital #
fn contains_capital(s string) bool
contains_capital reports whether s contains an uppercase character.
fn dev_dependencies_for_tool #
fn dev_dependencies_for_tool(tool_name string) []string
dev_dependencies_for_tool returns the modules from outside vlib that the bundled tool tool_name needs before it can be compiled, read from the tool's own v.mod. This replaces a table that the compiler carried, which meant a tool needing a module was invisible in the tool's own manifest and could not be declared without editing vlib.
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 a dev_dependencies entry in its own v.mod. 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 external dependencies in the tool's manifest.
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 #
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 v_literal_parse_base #
fn v_literal_parse_base(value string) int
v_literal_parse_base selects decimal parsing for V integer literals unless an explicit 0b, 0o, or 0x prefix requests base inference. It skips an optional sign.
fn verror #
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 ComptimeStringScalar #
struct ComptimeStringScalar {
pub:
typ u32
value string
}
ComptimeStringScalar is the result of a pure string operation on known operands.
fn (ComptimeStringScalar) typ_text #
fn (value &ComptimeStringScalar) typ_text() string
typ_text resolves the indexed source type spelling.
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 #
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 #
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 #
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 #
struct SuggestionParams {
pub mut:
similarity_threshold f32 = 0.5
similarity_fn CalculateSuggestionSimilarityFN = strings.dice_coefficient
}
SuggestionParams configures new_suggestion.
- README
- Constants
- fn args_quote_paths
- fn arm64_asm_register_names
- fn asm_clobber_is_special
- fn asm_register_names
- fn check_module_is_installed
- fn closest_asm_register
- fn comptime_string_bound
- fn comptime_string_index
- fn comptime_string_repeat
- fn comptime_string_scalar
- fn comptime_string_split
- fn contains_capital
- fn dev_dependencies_for_tool
- fn ensure_modules_for_all_tools_are_installed
- fn ensure_modules_for_tool_are_installed
- fn external_modules_for_tool
- fn find_all_v_files
- fn githash
- fn is_escape_sequence
- fn is_project_boundary_dir
- fn nearest_vmod_root
- fn new_suggestion
- fn parse_inline_asm_header
- fn path_styled_for_error_messages
- fn quote_path
- fn resolve_env_value
- fn set_vroot_folder
- fn tokenize_c_flag
- fn v_literal_parse_base
- fn verror
- fn x86_asm_register_names
- type CalculateSuggestionSimilarityFN
- type Suggestion
- struct ComptimeStringScalar
- struct InlineAsmHeader
- struct KeyRecentCache
- struct SuggestionParams