Talk:Trimming (computer programming): Difference between revisions

Content deleted Content added
Implementing WP:PIQA (Task 26)
 
(19 intermediate revisions by 12 users not shown)
Line 1:
{{WikiProject banner shell|class=Start|
{{WikiProject Computing|importance=low
|science=yes
|science-importance=low
}}
}}
 
== C/C++ example needs actual code ==
 
Line 6 ⟶ 13:
 
:: I've changed the code for the C example. I believe the old example would have leaked memory - for processing large amounts of data, this could cause problems! On a side note, could someone check to make sure I haven't done something dumb while writing this (it compiled and worked in VC++) --[[User:Portej|Portej]] 07:29, 26 September 2007 (UTC)
 
::: At least for me the example in C does '''NOT''' work. i doubled checked it, but still believe, that i did not make a mistake. the problematic section is shown below. this causes a segmentation fault in my environment (gcc-4.1.3). --[[User:Alex.blackbit|Alex.blackbit]] 18:32, 1 November 2007 (UTC)
<syntaxhighlight lang="c">
if (last != input)
last[1] = 0;
</syntaxhighlight>
 
:::: Alex, this is with strings that don't need trimming? Yeh, last should have been initialized with ret before the loop. I'm fine with just dropping the code though, if it wasn't just a line count thing. [[User:Nevyn|James Antill]] 16:03, 6 November 2007 (UTC)
 
----
 
in AWK better to use those command:
Line 26 ⟶ 43:
Hi, I think you should remove the "other languages which don't have trim functions" section. They '''don't''' have trim or a close analogue, so they should be trimmed. It is superfluous text at the end of the article that happens to take up more than half the article that is supposed to be on '''trim''' rather than what is not trim. It is quite easy to define trim-like and, by omision what is not trim-like. That section becomes a tutorial on how to write trim in other languages and should be removed or moved to this talk page. Please refer to [[What_Wikipedia_is_not#Wikipedia_is_not_a_manual.2C_guidebook.2C_or_textbook| this]]. --[[User:Paddy3118|Paddy]] 05:32, 10 October 2007 (UTC)
 
:It's about trim library functions as well as trim algorithms. Most of the examples (except C) are only one or two lines of code (which is probably why they don't have library functions). Hardly a hand-holding instruction manual. Leaving them out is to pretend trimming isn't used in these languages, and removes much of the language-specific discussion. Lastly, the code certainly doesn't belong on the talk page. —[[User:Pengo|Pengo]] 14:26, 10 October 2007 (UTC)
===Trim in other languages===
In languages without a built-in trim function, a custom function may need to be written, or a library found.
 
====AWK====
 
[[AWK programming language|AWK]] uses regular expressions to trim[http://www.faqs.org/qa/qa-1120.html] :
 
ltrim(v) = gsub(/^[ \t]+/, "", v)
rtrim(v) = gsub(/[ \t]+$/, "", v)
trim(v) = ltrim(v); rtrim(v)
 
or:
 
function ltrim(s) { sub(/^ */, "", s); return s }
function rtrim(s) { sub(/ *$/, "", s); return s }
function trim(s) { return rtrim(ltrim(s)); }
 
====C/C++====
There is no standard trim function in C or C++. The equivalent function has also often been called '''EatWhitespace''' in non-standard C libraries.
 
The [[open-source software|open source]] C++ library [[Boost library|Boost]] has several trim variants, including a standard one: [http://www.boost.org/doc/html/string_algo/usage.html#id2742817]
 
trimmed = boost::algorithm::trim_copy(''string'');
 
Note that with boost's function named simply <code>trim</code> the input sequence is modified in-place[http://www.boost.org/doc/html/trim.html], and does not return a result.
 
The [[Linux kernel]] also includes a strip function, <code>strstrip()</code>, since 2.6.18-rc1, which trims the string "in place".
 
The [[open-source software|open source]] and [[Porting|portable]] C and C++ library [[Better String Library|The Better String Library]] has support for trimming as well:
 
btrimws (b = bfromcstr (" string "));
 
The following C code snippet can be used to remove white spaces from a string:
 
<source lang="c">
char* trimWhitespace(char* inString);
int isWhitespace(char c);
 
int
isWhitespace(char c)
{
return c == ' ' || c == '\t' || c == '\v' || c == '\f';
}
 
char*
trimWhitespace(char* iS)
{
char *ret = 0;
int iSLength = 0, retLength = 0, sStart = 0, sEnd = 0;
int i = 0, n = 0;
 
iSLength = (int)strlen(iS);
 
// Find first non-whitespace character
while (sStart < iSLength && isWhitespace(iS[sStart]))
sStart++;
 
// Find last character before all whitespace
// Starting point is the last character
sEnd = iSLength - 1;
while(sEnd > 0 && isWhitespace(iS[sEnd]))
sEnd--;
 
// Create a new string and allocate the memory
retLength = (sEnd-sStart + 1) + 1; // Extra 1 is for the NULL terminator
ret = (char*)malloc(retLength * sizeof(char));
for (i = sStart; i <= sEnd; i++)
ret[n++] = iS[i];
ret[n] = `\0`;
 
return ret;
}
</source>
 
====Haskell====
 
A trim algorithm in [[Haskell (programming language)|Haskell]][http://www.haskell.org/haskellwiki/Simple_unix_tools]
 
import Data.Char
trim :: String -> String
trim = f . f
where f = reverse . dropWhile isSpace
 
::I see what you mean about the ''short'' examples, and also not transferring them to the talk pages, but that large chunk of C? Surely that should go. --[[User:Paddy3118|Paddy]] 15:32, 10 October 2007 (UTC)
may be interpreted as follows: ''f'' drops the preceding whitespace, and reverses the string. ''f'' is then again applied to its own output. Note that the type signature (the second line) is optional.
 
:::Yep, probably. Would be nice to move it to Wikibooks, or at least include a link to an open source implementation of the same thing though. —[[User:Pengo|Pengo]] 06:09, 11 October 2007 (UTC)
====JavaScript====
There is no built-in trim function, but it can be added to the String class [http://jibbering.com/faq/#FAQ4_16]:
 
:::The only point is that if that trim function was used on a large dataset, with lots of whitespace, it would leak memory at a heck of a rate. Is there anywhere that implementations can go? (someone mentioned wikibooks) <small>—Preceding [[Wikipedia:Signatures|unsigned]] comment added by [[Special:Contributions/203.59.18.181|203.59.18.181]] ([[User talk:203.59.18.181|talk]]) 14:37, 17 October 2007 (UTC)</small><!-- Template:UnsignedIP --> <!--Autosigned by SineBot-->
To add a trim function to all strings:
String.prototype.trim = function() {
return this.replace(/^\s*|\s*$/g, "")
}
 
::I dropped the line count significantly on the C example, and it now doesn't allocate memory. Both of which make it much more likely to resemble a real implementation IMO. --[[User:Nevyn|James Antill]] 05:32, 19 October 2007 (UTC)
This allows the same syntax as Java to be used for JavaScript.
 
:::Thanks, it is more succinct, but the point remains that the C function is showing how to write trim in C which does not have trim. Best to leave it out and rely on the mention of external libraries that give trim functionality as a package. --[[User:Paddy3118|Paddy]] 18:04, 19 October 2007 (UTC)
====Perl====
Perl has no built-in function, and a trimming is usually achieved through [[regular expression]]s.
 
:I've re-removed the C function, as it seems to be a perennial problem. The other code in the article is well established and used frequently in copy/paste programming. I don't think Wikipedia is the best environment to write new code. It could even be seen as "original research". —[[User:Pengo|Pengo]] 01:08, 2 November 2007 (UTC)
Example:
''$string'' =~ s/^\s+//; # remove leading whitespace
''$string'' =~ s/\s+$//; # remove trailing whitespace
or:
''$string'' =~ s/^\s+|\s+$//g ; # remove both leading and trailing whitespace
 
== SAS has a trimmed TRIM ==
These examples modify the value of the original variable <code>$string</code>.
 
the SAS language has a STRIP function, but this only removes the space (ASCII 0x20) characters, not all whitespace. <span style="font-size: smaller;" class="autosigned">—Preceding [[Wikipedia:Signatures|unsigned]] comment added by [[Special:Contributions/165.155.110.5|165.155.110.5]] ([[User talk:165.155.110.5|talk]]) 14:32, 8 February 2011 (UTC)</span><!-- Template:UnsignedIP --> <!--Autosigned by SineBot-->
Also available for Perl is '''StripLTSpace''' in <code>String::Strip</code> from [[CPAN]].
 
== External links modified ==
There are however two functions that are commonly used to strip whitespace from the end of strings, chomp and chop:
* chop removes the last character from a string and returns it.
* chomp removes the trailing newline from a string if present.
 
Hello fellow Wikipedians,
====Tcl====
The [[Tcl]] <code>string</code> command has three relevant subcommands: <code>trim</code>, <code>trimright</code> and <code>trimleft</code>. For each of those commands, an additional argument may be specified: a string that represents a set of characters to remove -- the default is whitespace (space, tab, newline, carriage return).
 
I have just added archive links to {{plural:1|one external link|1 external links}} on [[Trimming (computer programming)]]. Please take a moment to review [https://en.wikipedia.org/w/index.php?diff=prev&oldid=699922317 my edit]. If necessary, add {{tlx|cbignore}} after the link to keep me from modifying it. Alternatively, you can add {{tlx|nobots|deny{{=}}InternetArchiveBot}} to keep me off the page altogether. I made the following changes:
Example: trimming vowels
*Added archive https://web.archive.org/20090802041055/http://doc.trolltech.com:80/4.5/qstring.html to http://doc.trolltech.com/4.5/qstring.html#trimmed
set string onomatopoeia
set trimmed [string trim $string aeiou] ;# result is nomatop
set r_trimmed [string trimright $string aeiou] ;# result is onomatop
set l_trimmed [string trimleft $string aeiou] ;# result is nomatopoeia
 
When you have finished reviewing my changes, please set the ''checked'' parameter below to '''true''' to let others know.
====XSLT====
[[XSL Transformations|XSLT]] has the function <code>normalize-space(''string'')</code> which strips leading and trailing whitespace and also replaces any sequence of whitespace characters (including linebreaks) with a single space.
 
{{sourcecheck|checked=false}}
Example:
<xsl:variable name='trimmed'>
<xsl:value-of select='normalize-space(''string'')'/>
</xsl:variable>
 
Cheers.—[[User:Cyberbot II|<sup style="color:green;font-family:Courier">cyberbot II</sup>]]<small><sub style="margin-left:-14.9ex;color:green;font-family:Comic Sans MS">[[User talk:Cyberbot II|<span style="color:green">Talk to my owner</span>]]:Online</sub></small> 07:23, 15 January 2016 (UTC)
XSLT 2.0 also includes regular expressions, providing another mechanism to perform trimming.