Append replacement: Difference between revisions

From Kolmafia
Jump to navigation Jump to search
imported>StDoodle
mNo edit summary
imported>Bale
m Add paragraph breaks and rephrase for clarity
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{
{{
#vardefine:name|append_replacement}}{{
#vardefine:name|append_replacement(), append_tail}}{{
#vardefine:return_type|buffer}}{{
#vardefine:return_type|buffer}}{{


FunctionPage|
FunctionPage|
name={{#var:name}}|
name={{#var:name}}|
function_category=String Handling Routines|


function1={{Function|
function1={{Function|
name={{#var:name}}|
name=append_replacement|
aggregate={{#var:aggregate}}|
aggregate={{#var:aggregate}}|
return_type={{#var:return_type}}|
return_type={{#var:return_type}}|
return_also={{#var:return_also}}|
return_also={{#var:return_also}}|
parameter1={{Param|matcher|match_at}}|
parameter1={{Param|matcher|match}}|
parameter2={{Param|buffer|source}}|
parameter2={{Param|buffer|buff}}|
parameter3={{Param|string|replace}}|
parameter3={{Param|string|replace}}|
p1desc={{Pspan|match_at}} is ?|
}}|
p2desc={{Pspan|source}} is the original buffer to replacements in?|
function2={{Function|
p3desc={{Pspan|replace}} is what to replace matches with?|
name=append_tail|
aggregate={{#var:aggregate}}|
return_type={{#var:return_type}}|
return_also={{#var:return_also}}|
parameter1={{Param|matcher|match}}|
parameter2={{Param|buffer|buff}}|
p1desc={{Pspan|match}} is a matcher created with the original string and search pattern|
p2desc={{Pspan|buff}} is the buffer in which the modified string is being accumulated|
p3desc={{Pspan|replace}} is the replacement text for the current match of the search pattern, with the same meanings for special characters as in [[replace_all|replace_all()]]|
}}|
}}|


function_description=Returns a modified version of {{pspan|source}}, with all valid matches for {{pspan|match_at}} replaced with the text {{pspan|replace}}. Maybe?|
function_description=These two functions are the building blocks for a regular expression-based search and replace operation.  Most of the time, you can use the more straightforward [[replace_string|replace_string()]] or [[replace_all|replace_all()]] functions instead; the only time you actually need to use these functions is when the replacement text has to be generated in some special way, rather than being a constant. Both functions return {{Pspan|buff}}, for convenience.


needscode=yes|
To use these functions, you must have a matcher variable which has been created with the desired search pattern and original text, and a buffer variable in which the modified string will be built. 


see_also={{SeeAlso|append_tail}}|
</p><p>'''append_replacement()''' is only valid immediately after [[find|find()]] has returned true for {{Pspan|match}}. It will copy text into the buffer starting after the previous match (or the start of the original string) through the start of the current match, and will then append the {{Pspan|replace}} string. 
 
</p><p>'''append_tail()''' is normally called only once, after find() returns false. It will copy any remaining text into the buffer, after the final match.|
 
code1={{CodeSample|
title=Code Samples|
description=This sample will replace every 4-letter word in the input string with its uppercase version.|
code=
<syntaxhighlight>
string cap4(string input)
{
buffer b;
matcher m = create_matcher("\\b\\w{4}\\b", input);
while (m.find()) {
m.append_replacement(b, m.group().to_upper_case());
}
m.append_tail(b);
return b.to_string();
}
 
print(cap4("did this script work right?"));
// Output will be: did THIS script WORK right?
</syntaxhighlight>
}}|
 
see_also={{SeeAlso|create_matcher}}|
}}
}}
{{RFI|I have no idea if I'm even close on this one; please check.}}
 
[[Category:String Handling Routines]]

Latest revision as of 19:50, 12 August 2015

Function Syntax

buffer append_replacement(matcher match ,buffer buff ,string replace )

buffer append_tail(matcher match ,buffer buff )

  • match is a matcher created with the original string and search pattern
  • buff is the buffer in which the modified string is being accumulated
  • replace is the replacement text for the current match of the search pattern, with the same meanings for special characters as in replace_all()

These two functions are the building blocks for a regular expression-based search and replace operation. Most of the time, you can use the more straightforward replace_string() or replace_all() functions instead; the only time you actually need to use these functions is when the replacement text has to be generated in some special way, rather than being a constant. Both functions return buff, for convenience. To use these functions, you must have a matcher variable which has been created with the desired search pattern and original text, and a buffer variable in which the modified string will be built.

append_replacement() is only valid immediately after find() has returned true for match. It will copy text into the buffer starting after the previous match (or the start of the original string) through the start of the current match, and will then append the replace string.

append_tail() is normally called only once, after find() returns false. It will copy any remaining text into the buffer, after the final match.

Code Samples

This sample will replace every 4-letter word in the input string with its uppercase version.

string cap4(string input)
{
	buffer b;
	matcher m = create_matcher("\\b\\w{4}\\b", input);
	while (m.find()) {
		m.append_replacement(b, m.group().to_upper_case());
	}
	m.append_tail(b);
	return b.to_string();
}

print(cap4("did this script work right?"));
// Output will be: did THIS script WORK right?

See Also

create_matcher()