replace
Replace needle with replacement as many times as possible, writing to an output buffer which is assumed to be of
appropriate size. Use replacementSize to calculate an appropriate buffer size.
The input and output slices must not overlap.
The needle must not be empty.
Returns the number of replacements made.
Function parameters
Parameters
- T:type
- input:[]const T
- needle:[]const T
- replacement:[]const T
- output:[]T
Type definitions in this namespace
Types
- Alignment
- Stored as a power-of-two.
Detects and asserts if the std.mem.Allocator interface is violated by the caller
Functions
- ValidationAllocator
- Detects and asserts if the std.mem.Allocator interface is violated by the caller
- copyForwards
- Copy all of source into dest at position 0.
- copyBackwards
- Copy all of source into dest at position 0.
- zeroes
- Generally, Zig users are encouraged to explicitly initialize all fields of a struct explicitly rather than using this function.
- zeroInit
- Initializes all fields of the struct with their default value, or zero values if no default value is present.
- sortContext
- TODO: currently this just calls `insertionSortContext`.
- order
- Compares two slices of numbers lexicographically.
- orderZ
- Compares two many-item pointers with NUL-termination lexicographically.
- lessThan
- Returns true if lhs < rhs, false otherwise
- eql
- Returns true if and only if the slices have the same length and all elements
- indexOfDiff
- Compares two slices and returns the index of the first inequality.
- span
- Takes a sentinel-terminated pointer and returns a slice, iterating over the
- sliceTo
- Takes a pointer to an array, a sentinel-terminated pointer, or a slice and iterates searching for
- len
- Takes a sentinel-terminated pointer and iterates over the memory to find the
- allEqual
- Returns true if all elements in a slice are equal to the scalar value provided
- trimStart
- Remove a set of values from the beginning of a slice.
- trimLeft
- Deprecated: use `trimStart` instead.
- trimEnd
- Remove a set of values from the end of a slice.
- trimRight
- Deprecated: use `trimEnd` instead.
- trim
- Remove a set of values from the beginning and end of a slice.
- indexOfScalar
- Linear search for the index of a scalar value inside a slice.
- lastIndexOfScalar
- Linear search for the last index of a scalar value inside a slice.
- indexOfNone
- Find the first item in `slice` which is not contained in `values`.
- lastIndexOfNone
- Find the last item in `slice` which is not contained in `values`.
- indexOfNonePos
- Find the first item in `slice[start_index..]` which is not contained in `values`.
- lastIndexOfLinear
- Find the index in a slice of a sub-slice, searching from the end backwards.
- indexOfPosLinear
- Consider using `indexOfPos` instead of this, which will automatically use a
- lastIndexOf
- Find the index in a slice of a sub-slice, searching from the end backwards.
- indexOfPos
- Uses Boyer-Moore-Horspool algorithm on large inputs; `indexOfPosLinear` on small inputs.
- count
- Returns the number of needles inside the haystack
- containsAtLeast
- Returns true if the haystack contains expected_count or more needles
- containsAtLeastScalar
- Returns true if the haystack contains expected_count or more needles
- readVarInt
- Reads an integer from memory with size equal to bytes.len.
- readVarPackedInt
- Loads an integer from packed memory with provided bit_count, bit_offset, and signedness.
- readInt
- Reads an integer from memory with bit count specified by T.
- readPackedInt
- Loads an integer from packed memory.
- writeInt
- Writes an integer to memory, storing it in twos-complement.
- writePackedInt
- Stores an integer to packed memory.
- writeVarPackedInt
- Stores an integer to packed memory with provided bit_offset, bit_count, and signedness.
- byteSwapAllFields
- Swap the byte order of all the members of the fields of a struct
- tokenizeAny
- Returns an iterator that iterates over the slices of `buffer` that are not
- tokenizeSequence
- Returns an iterator that iterates over the slices of `buffer` that are not
- tokenizeScalar
- Returns an iterator that iterates over the slices of `buffer` that are not
- splitSequence
- Returns an iterator that iterates over the slices of `buffer` that
- splitAny
- Returns an iterator that iterates over the slices of `buffer` that
- splitScalar
- Returns an iterator that iterates over the slices of `buffer` that
- splitBackwardsSequence
- Returns an iterator that iterates backwards over the slices of `buffer` that
- splitBackwardsAny
- Returns an iterator that iterates backwards over the slices of `buffer` that
- splitBackwardsScalar
- Returns an iterator that iterates backwards over the slices of `buffer` that
- window
- Returns an iterator with a sliding window of slices for `buffer`.
- join
- Naively combines a series of slices with a separator.
- joinZ
- Naively combines a series of slices with a separator and null terminator.
- concat
- Copies each T from slices into a new slice that exactly holds all the elements.
- concatWithSentinel
- Copies each T from slices into a new slice that exactly holds all the elements.
- concatMaybeSentinel
- Copies each T from slices into a new slice that exactly holds all the elements as well as the sentinel.
- min
- Returns the smallest number in a slice.
- max
- Returns the largest number in a slice.
- minMax
- Finds the smallest and largest number in a slice.
- indexOfMin
- Returns the index of the smallest number in a slice.
- indexOfMax
- Returns the index of the largest number in a slice.
- indexOfMinMax
- Finds the indices of the smallest and largest number in a slice.
- reverse
- In-place order reversal of a slice
- reverseIterator
- Iterates over a slice in reverse.
- rotate
- In-place rotation of the values in an array ([0 1 2 3] becomes [1 2 3 0] if we rotate by 1)
- replace
- Replace needle with replacement as many times as possible, writing to an output buffer which is assumed to be of
- replaceScalar
- Replace all occurrences of `match` with `replacement`.
- collapseRepeatsLen
- Collapse consecutive duplicate elements into one entry.
- collapseRepeats
- Collapse consecutive duplicate elements into one entry.
- replacementSize
- Calculate the size needed in an output buffer to perform a replacement.
- replaceOwned
- Perform a replacement on an allocated buffer of pre-determined size.
- littleToNative
- Converts a little-endian integer to host endianness.
- bigToNative
- Converts a big-endian integer to host endianness.
- toNative
- Converts an integer from specified endianness to host endianness.
- nativeTo
- Converts an integer which has host endianness to the desired endianness.
- nativeToLittle
- Converts an integer which has host endianness to little endian.
- nativeToBig
- Converts an integer which has host endianness to big endian.
- alignPointerOffset
- Returns the number of elements that, if added to the given pointer, align it
- alignPointer
- Aligns a given pointer value to a specified alignment factor.
- asBytes
- Given a pointer to a single item, returns a slice of the underlying bytes, preserving pointer attributes.
- toBytes
- Given any value, returns a copy of its bytes in an array.
- bytesAsValue
- Given a pointer to an array of bytes, returns a pointer to a value of the specified type
- bytesToValue
- Given a pointer to an array of bytes, returns a value of the specified type backed by a
- bytesAsSlice
- Given a slice of bytes, returns a slice of the specified type
- sliceAsBytes
- Given a slice, returns a slice of the underlying bytes, preserving pointer attributes.
- alignForwardAnyAlign
- Round an address down to the next (or current) aligned address.
- alignForward
- Round an address up to the next (or current) aligned address.
- doNotOptimizeAway
- Force an evaluation of the expression; this tries to prevent
- alignBackwardAnyAlign
- Round an address down to the previous (or current) aligned address.
- alignBackward
- Round an address down to the previous (or current) aligned address.
- isValidAlign
- Returns whether `alignment` is a valid alignment, meaning it is
- isValidAlignGeneric
- Returns whether `alignment` is a valid alignment, meaning it is
- isAligned
- Given an address and an alignment, return true if the address is a multiple of the alignment
- alignInBytes
- Returns the largest slice in the given bytes that conforms to the new alignment,
- alignInSlice
- Returns the largest sub-slice within the given slice that conforms to the new alignment,
The standard library currently thoroughly depends on byte size
Values
- byte_size_in_bits
- The standard library currently thoroughly depends on byte size
- readPackedIntNative
- = switch (native_endian) { .little => readPackedIntLittle, .big => readPackedIntBig, }
- readPackedIntForeign
- = switch (native_endian) { .little => readPackedIntBig, .big => readPackedIntLittle, }
- writePackedIntNative
- = switch (native_endian) { .little => writePackedIntLittle, .big => writePackedIntBig, }
- writePackedIntForeign
- = switch (native_endian) { .little => writePackedIntBig, .big => writePackedIntLittle, }
Source
Implementation
pub fn replace(comptime T: type, input: []const T, needle: []const T, replacement: []const T, output: []T) usize {
// Empty needle will loop until output buffer overflows.
assert(needle.len > 0);
var i: usize = 0;
var slide: usize = 0;
var replacements: usize = 0;
while (slide < input.len) {
if (mem.startsWith(T, input[slide..], needle)) {
@memcpy(output[i..][0..replacement.len], replacement);
i += replacement.len;
slide += needle.len;
replacements += 1;
} else {
output[i] = input[slide];
i += 1;
slide += 1;
}
}
return replacements;
}