Difference between revisions of "Append replacement"

From Kolmafia
Jump to navigation Jump to search
imported>StDoodle
m
imported>Jasonharper
(Explainified!)
Line 1: Line 1:
 
{{
 
{{
#vardefine:name|append_replacement}}{{
+
#vardefine:name|append_replacement(), append_tail}}{{
 
#vardefine:return_type|buffer}}{{
 
#vardefine:return_type|buffer}}{{
  
Line 8: Line 8:
  
 
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?|
 
p3desc={{Pspan|replace}} is what to replace matches with?|
 
 
}}|
 
}}|
 +
function2={{Function|
 +
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=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.
 +
 +
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() (which is only valid immediately after [[find|find()]] has returned true for {{Pspan|match}}) 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.  append_tail() (which is normally called only once, after find() returns false) will copy any remaining text into the buffer, after the final match.  Both functions return {{Pspan|buff}}, for convenience.|
  
function_description=Returns a modified version of {{pspan|source}}, with all valid matches for {{pspan|match_at}} replaced with the text {{pspan|replace}}. Maybe?|
+
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();
 +
}
  
needscode=yes|
+
print(cap4("did this script work right?"));
 +
// Output will be: did THIS script WORK right?
 +
</syntaxhighlight>
 +
}}|
  
see_also={{SeeAlso|append_tail}}|
+
see_also={{SeeAlso|create_matcher}}|
 
}}
 
}}
{{RFI|I have no idea if I'm even close on this one; please check.}}
 

Revision as of 19:08, 12 March 2010

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. 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() (which is only valid immediately after find() has returned true for match) 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() (which is normally called only once, after find() returns false) will copy any remaining text into the buffer, after the final match. Both functions return buff, for convenience.

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()