String-Util-1.34/0000775000175000017500000000000014366507623013140 5ustar bakersbakersString-Util-1.34/README0000644000175000017500000001755414366507623014032 0ustar bakersbakersNAME String::Util -- String processing utility functions DESCRIPTION String::Util provides a collection of small, handy functions for processing strings in various ways. INSTALLATION cpanm String::Util USAGE No functions are exported by default, they must be specified: use String::Util qw(trim eqq contains) alternately you can use :all to export all of the functions use String::Util qw(:all) FUNCTIONS collapse($string) collapse() collapses all whitespace in the string down to single spaces. Also removes all leading and trailing whitespace. Undefined input results in undefined output. $var = collapse(" Hello world! "); # "Hello world!" hascontent($scalar), nocontent($scalar) hascontent() returns true if the given argument is defined and contains something besides whitespace. An undefined value returns false. An empty string returns false. A value containing nothing but whitespace (spaces, tabs, carriage returns, newlines, backspace) returns false. A string containing any other characters (including zero) returns true. nocontent() returns the negation of hascontent(). $var = hascontent(""); # False $var = hascontent(" "); # False $var = hascontent("a"); # True $var = nocontent(""); # True $var = nocontent("a"); # False trim($string), ltrim($string), rtrim($string) Returns the string with all leading and trailing whitespace removed. $var = trim(" my string "); # "my string" ltrim() trims leading whitespace only. rtrim() trims trailing whitespace only. nospace($string) Removes all whitespace characters from the given string. This includes spaces between words. $var = nospace(" Hello World! "); # "HelloWorld!" htmlesc($string) Formats a string for literal output in HTML. An undefined value is returned as an empty string. htmlesc() is very similar to CGI.pm's escapeHTML. However, there are a few differences. htmlesc() changes an undefined value to an empty string, whereas escapeHTML() returns undefs as undefs. jsquote($string) Escapes and quotes a string for use in JavaScript. Escapes single quotes and surrounds the string in single quotes. Returns the modified string. unquote($string) If the given string starts and ends with quotes, removes them. Recognizes single quotes and double quotes. The value must begin and end with same type of quotes or nothing is done to the value. Undef input results in undef output. Some examples and what they return: unquote(q|'Hendrix'|); # Hendrix unquote(q|"Hendrix"|); # Hendrix unquote(q|Hendrix|); # Hendrix unquote(q|"Hendrix'|); # "Hendrix' unquote(q|O'Sullivan|); # O'Sullivan option: braces If the braces option is true, surrounding braces such as [] and {} are also removed. Some examples: unquote(q|[Janis]|, braces=>1); # Janis unquote(q|{Janis}|, braces=>1); # Janis unquote(q|(Janis)|, braces=>1); # Janis repeat($string, $count) Returns the given string repeated the given number of times. The following command outputs "Fred" three times: print repeat('Fred', 3), "\n"; Note that repeat() was created a long time based on a misunderstanding of how the perl operator 'x' works. The following command using x would perform exactly the same as the above command. print 'Fred' x 3, "\n"; Use whichever you prefer. eqq($scalar1, $scalar2) Returns true if the two given values are equal. Also returns true if both are undef. If only one is undef, or if they are both defined but different, returns false. Here are some examples and what they return. $var = eqq('x', 'x'); # True $var = eqq('x', undef); # False $var = eqq(undef, undef); # True neqq($scalar1, $scalar2) The opposite of neqq, returns true if the two values are *not* the same. Here are some examples and what they return. $var = neqq('x', 'x'); # False $var = neqq('x', undef); # True $var = neqq(undef, undef); # False ords($string) Returns the given string represented as the ascii value of each character. $var = ords('Hendrix'); # {72}{101}{110}{100}{114}{105}{120} options * convert_spaces=>[true|false] If convert_spaces is true (which is the default) then spaces are converted to their matching ord values. So, for example, this code: $var = ords('a b', convert_spaces=>1); # {97}{32}{98} This code returns the same thing: $var = ords('a b'); # {97}{32}{98} If convert_spaces is false, then spaces are just returned as spaces. So this code: ords('a b', convert_spaces=>0); # {97} {98} * alpha_nums If the alpha_nums option is false, then characters 0-9, a-z, and A-Z are not converted. For example, this code: $var = ords('a=b', alpha_nums=>0); # a{61}b deords($string) Takes the output from ords() and returns the string that original created that output. $var = deords('{72}{101}{110}{100}{114}{105}{120}'); # 'Hendrix' contains($string, $substring) Checks if the string contains substring $var = contains("Hello world", "Hello"); # true $var = contains("Hello world", "llo wor"); # true $var = contains("Hello world", "QQQ"); # false # Also works with grep @arr = grep { contains("cat") } @input; startswith($string, $substring) Checks if the string starts with the characters in substring $var = startwith("Hello world", "Hello"); # true $var = startwith("Hello world", "H"); # true $var = startwith("Hello world", "Q"); # false # Also works with grep @arr = grep { startswith("X") } @input; endswith($string, $substring) Checks if the string ends with the characters in substring $var = endswith("Hello world", "world"); # true $var = endswith("Hello world", "d"); # true $var = endswith("Hello world", "QQQ"); # false # Also works with grep @arr = grep { endswith("z") } @input; crunchlines($string) Compacts contiguous newlines into single newlines. Whitespace between newlines is ignored, so that two newlines separated by whitespace is compacted down to a single newline. $var = crunchlines("x\n\n\nx"); # "x\nx"; sanitize($string, $separator = "_") Sanitize all non alpha-numeric characters in a string to underscores. This is useful to take a URL, or filename, or text description and know you can use it safely in a URL or a filename. Note: This will remove any trailing or leading '_' on the string $var = sanitize("http://www.google.com/") # http_www_google_com $var = sanitize("foo_bar()"; # foo_bar $var = sanitize("/path/to/file.txt"); # path_to_file_txt $var = sanitize("Big yellow bird!", "."); # Big.yellow.bird file_get_contents($string, $boolean) Read an entire file from disk into a string. Returns undef if the file cannot be read for any reason. Can also return the file as an array of lines. $str = file_get_contents("/tmp/file.txt"); # Return a string @lines = file_get_contents("/tmp/file.txt", 1); # Return an array Note: If you opt to return an array, carriage returns and line feeds are removed from the end of each line. Note: File is read in UTF-8 mode, unless $FGC_MODE is set to an appropriate encoding. COPYRIGHT AND LICENSE Copyright (c) 2012-2016 by Miko O'Sullivan. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. This software comes with NO WARRANTY of any kind. AUTHORS Miko O'Sullivan Scott Baker String-Util-1.34/Changes0000644000175000017500000000331514366507623014433 0ustar bakersbakers1.34 2023-02-01 08:13:06 PST 1.34 2023-02-01 - Re-release because the required Perl version was wrong 1.33 2023-01-31 - Remove a bunch of old deprecated functions: crunch, cellfill, define, randword, fullchomp, randcrypt, equndef, neundef - Update documentation 1.32 2021-03-26 09:27:35 PDT - Per bug reports, trim(undef) will return undef - Add a custom separtor option to sanitize 1.31 2020-07-27 10:28:07 PDT - Deprecate randcrypt() as it's not secure in 2020 - Add file_get_contents() - Make trim() unicode friendly 1.30 2020-07-23 14:29:05 PDT - Ownership transferred to Scott Baker - Removed undocumented function: spacepad() - Added a new sanitize() function - Deprecated some older functions: crunch(), fullchomp(), define(), and cellfill() 1.24 2014-12-31 - Cleaned up POD formatting. - Changed file to using Unixish style newlines. I hadn't realized until now that it was using Windowish newline. How embarrasing. - Added some features to ords(). 1.23 - Fixed error in META.yml. 1.22 - Fix in documentation for randpost(). - Clarified documentation for hascontent() and nocontent(). 1.21 2012-07-18 - Fixed error in POD. Tightened up code for repet. 1.20 2012-07-14 - Properly listing prerequisites. 1.01 2010-11-07 - Decided it was time to upload five years worth of changes. 0.11 2005-12-22 - This is a non-backwards compatible version. - urldecode, urlencode were removed entirely. All of the subs that used to modify values in place were changed so that they do not do so anymore, except for fullchomp. - See http://www.xray.mpe.mpg.de/mailing-lists/modules/2005-12/msg00112.html for why these changes were made. 0.10 2005-12-01 - Initial release String-Util-1.34/LICENSE0000644000175000017500000004370014366507623014147 0ustar bakersbakersThis software is copyright (c) 2012-2016 by Miko O'Sullivan. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Terms of the Perl programming language system itself a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or b) the "Artistic License" --- The GNU General Public License, Version 1, February 1989 --- This software is Copyright (c) 2012-2016 by Miko O'Sullivan. This is free software, licensed under: The GNU General Public License, Version 1, February 1989 GNU GENERAL PUBLIC LICENSE Version 1, February 1989 Copyright (C) 1989 Free Software Foundation, Inc. 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The license agreements of most software companies try to keep users at the mercy of those companies. By contrast, our General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. The General Public License applies to the Free Software Foundation's software and to any other program whose authors commit to using it. You can use it for your programs, too. When we speak of free software, we are referring to freedom, not price. Specifically, the General Public License is designed to make sure that you have the freedom to give away or sell copies of free software, that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of a such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must tell them their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any work containing the Program or a portion of it, either verbatim or with modifications. Each licensee is addressed as "you". 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this General Public License and to the absence of any warranty; and give any other recipients of the Program a copy of this General Public License along with the Program. You may charge a fee for the physical act of transferring a copy. 2. You may modify your copy or copies of the Program or any portion of it, and copy and distribute such modifications under the terms of Paragraph 1 above, provided that you also do the following: a) cause the modified files to carry prominent notices stating that you changed the files and the date of any change; and b) cause the whole of any work that you distribute or publish, that in whole or in part contains the Program or any part thereof, either with or without modifications, to be licensed at no charge to all third parties under the terms of this General Public License (except that you may choose to grant warranty protection to some or all third parties, at your option). c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the simplest and most usual way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this General Public License. d) You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. Mere aggregation of another independent work with the Program (or its derivative) on a volume of a storage or distribution medium does not bring the other work under the scope of these terms. 3. You may copy and distribute the Program (or a portion or derivative of it, under Paragraph 2) in object code or executable form under the terms of Paragraphs 1 and 2 above provided that you also do one of the following: a) accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Paragraphs 1 and 2 above; or, b) accompany it with a written offer, valid for at least three years, to give any third party free (except for a nominal charge for the cost of distribution) a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Paragraphs 1 and 2 above; or, c) accompany it with the information you received as to where the corresponding source code may be obtained. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form alone.) Source code for a work means the preferred form of the work for making modifications to it. For an executable file, complete source code means all the source code for all modules it contains; but, as a special exception, it need not include source code for modules which are standard libraries that accompany the operating system on which the executable file runs, or for standard header files or definitions files that accompany that operating system. 4. You may not copy, modify, sublicense, distribute or transfer the Program except as expressly provided under this General Public License. Any attempt otherwise to copy, modify, sublicense, distribute or transfer the Program is void, and will automatically terminate your rights to use the Program under this License. However, parties who have received copies, or rights to use copies, from you under this General Public License will not have their licenses terminated so long as such parties remain in full compliance. 5. By copying, distributing or modifying the Program (or any work based on the Program) you indicate your acceptance of this license to do so, and all its terms and conditions. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. 7. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of the license which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the license, you may choose any version ever published by the Free Software Foundation. 8. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS Appendix: How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to humanity, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 19yy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19xx name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (a program to direct compilers to make passes at assemblers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice That's all there is to it! --- The Artistic License 1.0 --- This software is Copyright (c) 2012-2016 by Miko O'Sullivan. This is free software, licensed under: The Artistic License 1.0 The Artistic License Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some semblance of artistic control over the development of the package, while giving the users of the package the right to use and distribute the Package in a more-or-less customary fashion, plus the right to make reasonable modifications. Definitions: - "Package" refers to the collection of files distributed by the Copyright Holder, and derivatives of that collection of files created through textual modification. - "Standard Version" refers to such a Package if it has not been modified, or has been modified in accordance with the wishes of the Copyright Holder. - "Copyright Holder" is whoever is named in the copyright or copyrights for the package. - "You" is you, if you're thinking about copying or distributing this Package. - "Reasonable copying fee" is whatever you can justify on the basis of media cost, duplication charges, time of people involved, and so on. (You will not be required to justify it to the Copyright Holder, but only to the computing community at large as a market that must bear the fee.) - "Freely Available" means that no fee is charged for the item itself, though there may be fees involved in handling the item. It also means that recipients of the item may redistribute it under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may apply bug fixes, portability fixes and other modifications derived from the Public Domain or from the Copyright Holder. A Package modified in such a way shall still be considered the Standard Version. 3. You may otherwise modify your copy of this Package in any way, provided that you insert a prominent notice in each changed file stating how and when you changed that file, and provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or placing the modifications on a major archive site such as ftp.uu.net, or by allowing the Copyright Holder to include your modifications in the Standard Version of the Package. b) use the modified Package only within your corporation or organization. c) rename any non-standard executables so the names do not conflict with standard executables, which must also be provided, and provide a separate manual page for each non-standard executable that clearly documents how it differs from the Standard Version. d) make other distribution arrangements with the Copyright Holder. 4. You may distribute the programs of this Package in object code or executable form, provided that you do at least ONE of the following: a) distribute a Standard Version of the executables and library files, together with instructions (in the manual page or equivalent) on where to get the Standard Version. b) accompany the distribution with the machine-readable source of the Package with your modifications. c) accompany any non-standard executables with their corresponding Standard Version executables, giving the non-standard executables non-standard names, and clearly documenting the differences in manual pages (or equivalent), together with instructions on where to get the Standard Version. d) make other distribution arrangements with the Copyright Holder. 5. You may charge a reasonable copying fee for any distribution of this Package. You may charge any fee you choose for support of this Package. You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution provided that you do not advertise this Package as a product of your own. 6. The scripts and library files supplied as input to or produced as output from the programs of this Package do not automatically fall under the copyright of this Package, but belong to whomever generated them, and may be sold commercially, and may be aggregated with this Package. 7. C or perl subroutines supplied by you and linked into this Package shall not be considered part of this Package. 8. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. The End String-Util-1.34/cpanfile0000644000175000017500000000010214366507623014633 0ustar bakersbakersrequires 'perl' => 'v5.14'; test_requires 'Test::More' => '0.88'; String-Util-1.34/dist.ini0000644000175000017500000000033214366507623014600 0ustar bakersbakerscopyright_holder = Miko O'Sullivan copyright_year = 2012-2016 license = Perl_5 author = Scott Baker author = Miko O'Sullivan [@Milla] -remove = LicenseFromModule String-Util-1.34/t/0000775000175000017500000000000014366507623013403 5ustar bakersbakersString-Util-1.34/t/test.t0000644000175000017500000001646414366507623014560 0ustar bakersbakers#!/usr/bin/perl -w use strict; use warnings; use String::Util ':all'; use Test::More; # general purpose variable my $val; #------------------------------------------------------------------------------ # crunch # # basic crunching ok(collapse(" Starflower \n\n\t Miko ") eq 'Starflower Miko', 'Basic collapse'); # collapse on undef returns undef ok(!defined collapse(undef), 'collapse undef should return undef'); # # crunch #------------------------------------------------------------------------------ is(crunchlines("x\n\n\nx"), "x\nx", "crunchlines with three \\ns"); is(crunchlines("x\nx") , "x\nx", "crunchlines with one \\ns"); is(crunchlines(undef) , undef , "crunchlines with undef"); #------------------------------------------------------------------------------ # hascontent # is(hascontent(undef), 0, "hascontent undef"); ok(!hascontent('') , "hascontent ''"); ok(!hascontent(" \t \n\n \r \n\n\r ") , "hascontent whitespace string"); ok(hascontent("0") , "hascontent zero"); ok(hascontent(" x ") , "hascontent string with x"); ok(nocontent("") , "nocontent ''"); ok(nocontent(" ") , "nocontent space"); ok(nocontent(undef) , "nocontent undef"); ok(!nocontent('a') , "nocontent char"); ok(!nocontent(' b ') , "nocontent char with spaces"); ok(!nocontent('word'), "nocontent word"); # # hascontent #------------------------------------------------------------------------------ #------------------------------------------------------------------------------ # trim # # basic trimming is(trim(undef) , undef , 'trim undef'); is(trim(" Perl ") , "Perl" , 'trim spaces'); is(trim("\t\tPerl\t\t") , "Perl" , 'trim tabs'); is(trim("\n\n\nPerl") , "Perl" , 'trim \n'); is(trim("\n\n\t\nPerl \t\n") , "Perl" , 'trim all three'); is(ltrim("\n\n\t\nPerl "), "Perl " , 'ltrim'); is(ltrim(undef) , undef, 'ltrim undef'); is(rtrim("\n\tPerl ") , "\n\tPerl" , 'rtrim'); is(rtrim(undef) , undef, 'rtrim undef'); # # trim #------------------------------------------------------------------------------ #------------------------------------------------------------------------------ # no_space # is(nospace(" ok \n fine "), 'okfine', 'nospace with whitespace'); is(nospace("Perl") , 'Perl' , 'nospace no whitespace'); is(nospace(undef) , undef , 'nospace undef'); # # no_space #------------------------------------------------------------------------------ #------------------------------------------------------------------------------ # startswith $val = "Quick brown fox"; ok(startswith("Quick brown fox" , 'Q') , "Startswidth char"); ok(startswith("Quick brown fox" , 'Quick') , "Startswidth word"); ok(!startswith("Quick brown fox", 'z') , "Does NOT start with char"); ok(!startswith("Quick brown fox", 'Qqq') , "Does NOT start with string"); is(startswith(undef, 'foo') , undef , "Startswidth undef"); #------------------------------------------------------------------------------ #------------------------------------------------------------------------------ # endswith $val = "Quick brown fox"; ok(endswith($val, 'x') , "Endswith char"); ok(endswith($val, 'fox') , "Endswith word"); ok(endswith($val, ' fox') , "Endswith space word"); ok(!endswith($val, 'foq') , "Does not end with string"); is(endswith(undef, 'foo'), undef , "Endswith undef"); #------------------------------------------------------------------------------ #------------------------------------------------------------------------------ # contains $val = "Quick brown fox"; ok(contains($val, 'brown') , "Contains word"); ok(contains($val, 'uick') , "Contains word 2"); ok(contains($val, 'n f') , "Contains word with space"); ok(!contains($val, 'bri') , "Does not contains word"); is(contains(undef, 'foo') , undef , "Contains undef"); #------------------------------------------------------------------------------ #------------------------------------------------------------------------------ # htmlesc # # basic operation of htmlesc is(htmlesc('<>"&') , '<>"&' , 'htmlesc special chars'); is(htmlesc(undef) , '' , 'htmlesc undef'); # # htmlesc #------------------------------------------------------------------------------ #------------------------------------------------------------------------------ # cellfill # # space-only string #is(cellfill(' '), ' ', 'cellfill spaces'); # undef string #is(cellfill(undef), ' ', 'cellfill undef'); # string with content #is(cellfill('x'), 'x', 'cellfill undef'); # # cellfill #------------------------------------------------------------------------------ #------------------------------------------------------------------------------ # eqq, neqq # ok(eqq('a' , 'a') , 'eqq same'); ok(eqq(undef , undef) , 'eqq undef'); ok(!eqq('a' , 'b') , 'eqq diff'); ok(!eqq('a' , undef) , 'eqq a and undef'); ok(!neqq('a' , 'a') , 'neqq same'); ok(!neqq(undef , undef) , 'neqq undef'); ok(neqq('a' , 'b') , 'neqq diff'); ok(neqq('a' , undef) , 'neqq a and undef'); # # eq_undef, neqq #------------------------------------------------------------------------------ #------------------------------------------------------------------------------ # define # # define an undef #is(define(undef), '', 'define undef'); # define an already defined value #is(define('x'), 'x', 'define string'); # # define #------------------------------------------------------------------------------ #------------------------------------------------------------------------------ # unquote # # single quotes is(unquote("'Starflower'") , 'Starflower' , 'unquote single quotes'); # double quotes is(unquote('"Starflower"') , 'Starflower' , 'unquote double quotes'); # no quotes is(unquote('Starflower') , 'Starflower' , 'unquote no quotes'); # Quote in middle is(unquote("Don't lets start") , "Don't lets start", 'unquote with quote in middle'); # # unquote #------------------------------------------------------------------------------ #------------------------------------------------------------------------------ # jsquote # is(jsquote("'yeah\n'"), q|'\'yeah\n<' + '/script>\''|, 'jsquote'); # # jsquote #------------------------------------------------------------------------------ #------------------------------------------------------------------------------ # fullchomp # # scalar context #is(fullchomp("Starflower\n\r\r\r\n"), 'Starflower', 'fullchomp'); # # fullchomp #------------------------------------------------------------------------------ is(sanitize("http://www.google.com/"), 'http_www_google_com', 'Sanitize URL'); is(sanitize("foo_bar()") , 'foo_bar' , 'Sanitize function name'); is(sanitize("/path/to/file.txt") , 'path_to_file_txt' , 'Sanitize path'); is(sanitize("Hello there!!!", '.') , 'Hello.there' , 'Sanitize with a custom separator'); # # randword #------------------------------------------------------------------------------ # file_get_contents() $val = file_get_contents(__FILE__); my @arr = file_get_contents(__FILE__, 1); ok(length($val) > 100, "file_get_contents string"); ok(@arr > 10 , "file_get_contents array"); done_testing(); String-Util-1.34/t/author-pod-syntax.t0000644000175000017500000000045414366507623017177 0ustar bakersbakers#!perl BEGIN { unless ($ENV{AUTHOR_TESTING}) { print qq{1..0 # SKIP these tests are for testing by the author\n}; exit } } # This file was automatically generated by Dist::Zilla::Plugin::PodSyntaxTests. use strict; use warnings; use Test::More; use Test::Pod 1.41; all_pod_files_ok(); String-Util-1.34/Build.PL0000644000175000017500000000026014366507623014430 0ustar bakersbakers# This Build.PL for String-Util was generated by Dist::Zilla::Plugin::ModuleBuildTiny 0.015. use strict; use warnings; use v5.14.0; use Module::Build::Tiny 0.034; Build_PL(); String-Util-1.34/META.yml0000644000175000017500000000205114366507623014405 0ustar bakersbakers--- abstract: 'String processing utility functions' author: - 'Scott Baker ' - "Miko O'Sullivan " build_requires: Test::More: '0.88' configure_requires: Module::Build::Tiny: '0.034' dynamic_config: 0 generated_by: 'Dist::Milla version v1.0.21, Dist::Zilla version 6.030, CPAN::Meta::Converter version 2.150010' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: String-Util no_index: directory: - eg - examples - inc - share - t - xt requires: perl: v5.14.0 resources: bugtracker: https://github.com/scottchiefbaker/String-Util/issues homepage: https://github.com/scottchiefbaker/String-Util repository: https://github.com/scottchiefbaker/String-Util.git version: '1.34' x_contributors: - 'Dan Book ' - 'Scott Baker ' x_generated_by_perl: v5.26.3 x_serialization_backend: 'YAML::Tiny version 1.73' x_spdx_expression: 'Artistic-1.0-Perl OR GPL-1.0-or-later' x_static_install: 1 String-Util-1.34/MANIFEST0000644000175000017500000000034114366507623014265 0ustar bakersbakers# This file was automatically generated by Dist::Zilla::Plugin::Manifest v6.030. Build.PL Changes LICENSE MANIFEST META.json META.yml README cpanfile dist.ini docs/index.html lib/String/Util.pm t/author-pod-syntax.t t/test.t String-Util-1.34/META.json0000644000175000017500000000347214366507623014565 0ustar bakersbakers{ "abstract" : "String processing utility functions", "author" : [ "Scott Baker ", "Miko O'Sullivan " ], "dynamic_config" : 0, "generated_by" : "Dist::Milla version v1.0.21, Dist::Zilla version 6.030, CPAN::Meta::Converter version 2.150010", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : 2 }, "name" : "String-Util", "no_index" : { "directory" : [ "eg", "examples", "inc", "share", "t", "xt" ] }, "prereqs" : { "configure" : { "requires" : { "Module::Build::Tiny" : "0.034" } }, "develop" : { "requires" : { "Dist::Milla" : "v1.0.21", "Test::Pod" : "1.41" } }, "runtime" : { "requires" : { "perl" : "v5.14.0" } }, "test" : { "requires" : { "Test::More" : "0.88" } } }, "release_status" : "stable", "resources" : { "bugtracker" : { "web" : "https://github.com/scottchiefbaker/String-Util/issues" }, "homepage" : "https://github.com/scottchiefbaker/String-Util", "repository" : { "type" : "git", "url" : "https://github.com/scottchiefbaker/String-Util.git", "web" : "https://github.com/scottchiefbaker/String-Util" } }, "version" : "1.34", "x_contributors" : [ "Dan Book ", "Scott Baker " ], "x_generated_by_perl" : "v5.26.3", "x_serialization_backend" : "Cpanel::JSON::XS version 4.28", "x_spdx_expression" : "Artistic-1.0-Perl OR GPL-1.0-or-later", "x_static_install" : 1 } String-Util-1.34/docs/0000775000175000017500000000000014366507623014070 5ustar bakersbakersString-Util-1.34/docs/index.html0000644000175000017500000004154114366507623016070 0ustar bakersbakers String::Util -- String processing utilities

NAME

String::Util -- String processing utilities

SYNOPSIS

  use String::Util ':all';

  # "crunch" whitespace and remove leading/trailing whitespace
  $val = crunch($val);

  # does this value have "content", i.e. it's defined
  # and has something besides whitespace?
  if (hascontent $val) {...}

  # format for display in a web page
  $val = htmlesc($val);

  # format for display in a web page table cell
  $val = cellfill($val);

  # remove leading/trailing whitespace
  $val = trim($val);

  # ensure defined value
  $val = define($val);

  # repeat string x number of times
  $val = repeat($val, $iterations);

  # remove leading/trailing quotes
  $val = unquote($val);

  # remove all whitespace
  $val = no_space($val);

  # remove trailing \r and \n, regardless of what
  # the OS considers an end-of-line
  $val = fullchomp($val);

  # or call in void context:
  fullchomp $val;

  # encrypt string using random seed
  $val = randcrypt($val);

  # are these two values equal, where two undefs count as "equal"?
  if (eqq $a, $b) {...}

  # are these two values different, where two undefs count as "equal"?
  if (neqq $a, $b) {...}

  # get a random string of some specified length
  $val = randword(10);

DESCRIPTION

String::Util provides a collection of small, handy utilities for processing strings.

INSTALLATION

String::Util can be installed with the usual routine:

 perl Makefile.PL
 make
 make test
 make install

FUNCTIONS

collapse($string), crunch($string)

collapse() collapses all whitespace in the string down to single spaces. Also removes all leading and trailing whitespace. Undefined input results in undefined output.

crunch() is the old name for collapse(). I decided that "crunch" never sounded right. Spaces don't go "crunch", they go "poof" like a collapsing ballon. However, crunch() will continue to work as an alias for collapse().

hascontent($scalar), nocontent($scalar)

hascontent() returns true if the given argument is defined and contains something besides whitespace.

An undefined value returns false. An empty string returns false. A value containing nothing but whitespace (spaces, tabs, carriage returns, newlines, backspace) returns false. A string containing any other characters (including zero) returns true.

nocontent() returns the negation of hascontent().

trim($string)

Returns the string with all leading and trailing whitespace removed. Trim on undef returns "".

So, for example, the following code changes " my string " to "my string":

 $var = " my string  ";
 $var = trim($var);

trim accepts two optional arguments, 'left' and 'right', both of which are true by default. So, to avoid trimming the left side of the string, set the 'left' argument to false:

 $var = trim($var, left=>0);

To avoid trimming the right side, set 'right' to false:

 $var = trim($var, right=>0);

ltrim, rtrim

ltrim trims leading whitespace. rtrim trims trailing whitespace. They are exactly equivalent to

 trim($var, left=>0);

and

 trim($var, right=>0);

no_space($string)

Removes all whitespace characters from the given string.

htmlesc(string)

Formats a string for literal output in HTML. An undefined value is returned as an empty string.

htmlesc() is very similar to CGI.pm's escapeHTML. However, there are a few differences. htmlesc() changes an undefined value to an empty string, whereas escapeHTML() returns undefs as undefs.

cellfill($string)

Formats a string for literal output in an HTML table cell. Works just like htmlesc() except that strings with no content (i.e. are undef or are just whitespace) are returned as &nbsp;.

jsquote($string)

Escapes and quotes a string for use in JavaScript. Escapes single quotes and surrounds the string in single quotes. Returns the modified string.

unquote($string)

If the given string starts and ends with quotes, removes them. Recognizes single quotes and double quotes. The value must begin and end with same type of quotes or nothing is done to the value. Undef input results in undef output. Some examples and what they return:

 unquote(q|'Hendrix'|);   # Hendrix
 unquote(q|"Hendrix"|);   # Hendrix
 unquote(q|Hendrix|);     # Hendrix
 unquote(q|"Hendrix'|);   # "Hendrix'
 unquote(q|O'Sullivan|);  # O'Sullivan

option: braces

If the braces option is true, surrounding braces such as [] and {} are also removed. Some examples:

 unquote(q|[Janis]|, braces=>1);  # Janis
 unquote(q|{Janis}|, braces=>1);  # Janis
 unquote(q|(Janis)|, braces=>1);  # Janis

define($scalar)

Takes a single value as input. If the value is defined, it is returned unchanged. If it is not defined, an empty string is returned.

This subroutine is useful for printing when an undef should simply be represented as an empty string. Perl already treats undefs as empty strings in string context, but this subroutine makes the warnings module go away. And you ARE using warnings, right?

repeat($string, $count)

Returns the given string repeated the given number of times. The following command outputs "Fred" three times:

 print repeat('Fred', 3), "\n";

Note that repeat() was created a long time based on a misunderstanding of how the perl operator 'x' works. The following command using 'x' would perform exactly the same as the above command.

 print 'Fred' x 3, "\n";

Use whichever you prefer.

randword($length, %options)

Returns a random string of characters. String will not contain any vowels (to avoid distracting dirty words). First argument is the length of the return string. So this code:

 foreach my $idx (1..3) {
   print randword(4), "\n";
 }

would output something like this:

 kBGV
 NCWB
 3tHJ

If the string 'dictionary' is sent instead of an integer, then a word is randomly selected from a dictionary file. By default, the dictionary file is assumed to be at /usr/share/dict/words and the shuf command is used to pull out a word. The hash %String::Util::PATHS sets the paths to the dictionary file and the shuf executable. Modify that hash to change the paths. So this code:

 foreach my $idx (1..3) {
   print randword('dictionary'), "\n";
 }

would output something like this:

 mustache
 fronds
 browning

option: alpha

If the alpha option is true, only alphabetic characters are returned, no numerals. For example, this code:

 foreach my $idx (1..3) {
   print randword(4, alpha=>1), "\n";
 }

would output something like this:

 qrML
 wmWf
 QGvF

option: numerals

If the numerals option is true, only numerals are returned, no alphabetic characters. So this code:

 foreach my $idx (1..3) {
   print randword(4, numerals=>1), "\n";
 }

would output something like this:

 3981
 4734
 2657

option: strip_vowels

This option is true by default. If true, vowels are not included in the returned random string. So this code:

 foreach my $idx (1..3) {
   print randword(4, strip_vowels=>1), "\n";
  }

would output something like this:

 Sk3v
 pV5z
 XhSX

eqq($val1, $val2)

Returns true if the two given values are equal. Also returns true if both are undef. If only one is undef, or if they are both defined but different, returns false. Here are some examples and what they return.

 eqq('x', 'x'), "\n";      # 1
 eqq('x', undef), "\n";    # 0
 eqq(undef, undef), "\n";  # 1

neqq($string1, $string2)

The opposite of neqq, returns true if the two values are *not* the same. Here are some examples and what they return.

 print neqq('x', 'x'), "\n";      # 0
 print neqq('x', undef), "\n";    # 1
 print neqq(undef, undef), "\n";  # 0

equndef(), neundef()

equndef() has been renamed to eqq(). neundef() has been renamed to neqq(). Those old names have been kept as aliases.

fullchomp($string)

Works like chomp, but is a little more thorough about removing \n's and \r's even if they aren't part of the OS's standard end-of-line.

Undefs are returned as undefs.

randcrypt($string)

Crypts the given string, seeding the encryption with a random two character seed.

randpost(%opts)

Returns a string that sorta looks like one or more paragraphs.

option: word_count

Sets how many words should be in the post. By default a random number from 1 to 250 is used.

option: par_odds

Sets the odds of starting a new paragraph after any given word. By default the value is .05, which means paragraphs will have an average about twenty words.

option: par

Sets the string to put at the end or the start and end of a paragraph. Defaults to two newlines for the end of a pargraph.

If this option is a single scalar, that string is added to the end of each paragraph.

To set both the start and end string, use an array reference. The first element should be the string to put at the start of a paragraph, the second should be the string to put at the end of a paragraph.

option: max_length

Sets the maximum length of the returned string, including paragraph delimiters.

ords($string)

Returns the given string represented as the ascii value of each character.

For example, this code:

 ords('Hendrix')

returns this string:

 {72}{101}{110}{100}{114}{105}{120}

options

  • convert_spaces=>[true|false]

    If convert_spaces is true (which is the default) then spaces are converted to their matching ord values. So, for example, this code:

     ords('a b', convert_spaces=>1)

    returns this:

    {97}{32}{98}

    This code returns the same thing:

     ords('a b')

    If convert_spaces is false, then spaces are just returned as spaces. So this code:

     ords('a b', convert_spaces=>0);

    returns

     {97} {98}
  • alpha_nums

    If the alpha_nums option is false, then characters 0-9, a-z, and A-Z are not converted. For example, this code:

     ords('a=b', alpha_nums=>0)

    returns this:

     a{61}b

deords($string)

Takes the output from ords() and returns the string that original created that output.

For example, this command:

 deords('{72}{101}{110}{100}{114}{105}{120}')

returns this string: Hendrix

contains($string, $substring)

Checks if the string contains substring

Examples:

  contains("Hello world", "Hello");   # true
  contains("Hello world", "llo wor"); # true
  contains("Hello world", "QQQ");     # false

startswith($string, $substring)

Checks if the string starts with the characters in substring

Examples:

  startwidth("Hello world", "Hello"); # true
  startwidth("Hello world", "H");     # true
  startwidth("Hello world", "Q");     # false

endswith($string, $substring)

Checks if the string ends with the characters in substring

Examples:

  endswidth("Hello world", "world");   # true
  endswidth("Hello world", "d");       # true
  endswidth("Hello world", "QQQ");     # false

crunchlines($string)

Compacts contiguous newlines into single newlines. Whitespace between newlines is ignored, so that two newlines separated by whitespace is compacted down to a single newline.

For example, this code:

 crunchlines("x\n\n\nx")

outputs two x's with a single empty line between them:

 x

 x

TERMS AND CONDITIONS

Copyright (c) 2012-2016 by Miko O'Sullivan. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. This software comes with NO WARRANTY of any kind.

AUTHORS

Miko O'Sullivan miko@idocs.com

Scott Baker scott@perturb.org

String-Util-1.34/lib/0000775000175000017500000000000014366507623013706 5ustar bakersbakersString-Util-1.34/lib/String/0000775000175000017500000000000014366507623015154 5ustar bakersbakersString-Util-1.34/lib/String/Util.pm0000644000175000017500000004133314366507623016431 0ustar bakersbakerspackage String::Util; use strict; use warnings; use Carp; use v5.14; # version our $VERSION = '1.34'; our $FGC_MODE = 'UTF-8'; #------------------------------------------------------------------------------ # opening POD # =head1 NAME B -- String processing utility functions =head1 DESCRIPTION B provides a collection of small, handy functions for processing strings in various ways. =head1 INSTALLATION cpanm String::Util =head1 USAGE No functions are exported by default, they must be specified: use String::Util qw(trim eqq contains) alternately you can use C<:all> to export B of the functions use String::Util qw(:all) =head1 FUNCTIONS =cut # # opening POD #------------------------------------------------------------------------------ #------------------------------------------------------------------------------ # export # use base 'Exporter'; use vars qw[@EXPORT_OK %EXPORT_TAGS]; # the following functions accept a value and return a modified version of # that value push @EXPORT_OK, qw[ collapse htmlesc trim ltrim rtrim repeat unquote no_space nospace jsquote crunchlines file_get_contents ]; # the following functions return true or false based on their input push @EXPORT_OK, qw[ hascontent nocontent eqq neqq startswith endswith contains sanitize ]; # the following function returns the unicode values of a string push @EXPORT_OK, qw[ ords deords ]; %EXPORT_TAGS = ('all' => [@EXPORT_OK]); # # export #------------------------------------------------------------------------------ #------------------------------------------------------------------------------ # collapse # =head2 collapse($string) C collapses all whitespace in the string down to single spaces. Also removes all leading and trailing whitespace. Undefined input results in undefined output. $var = collapse(" Hello world! "); # "Hello world!" =cut sub collapse { my ($val) = @_; if (defined $val) { $val =~ s|^\s+||s; $val =~ s|\s+$||s; $val =~ s|\s+| |sg; } return $val; } # # collapse #------------------------------------------------------------------------------ #------------------------------------------------------------------------------ # hascontent # =head2 hascontent($scalar), nocontent($scalar) C returns true if the given argument is defined and contains something besides whitespace. An undefined value returns false. An empty string returns false. A value containing nothing but whitespace (spaces, tabs, carriage returns, newlines, backspace) returns false. A string containing any other characters (including zero) returns true. C returns the negation of C. $var = hascontent(""); # False $var = hascontent(" "); # False $var = hascontent("a"); # True $var = nocontent(""); # True $var = nocontent("a"); # False =cut sub hascontent { my $val = shift(); if (!defined($val)) { return 0; } # If there are ANY non-space characters in it if ($val =~ m|\S|s) { return 1; } return 0; } sub nocontent { my $str = shift(); # nocontent() is just the inverse to hascontent() my $ret = !(hascontent($str)); return $ret; } # # hascontent #------------------------------------------------------------------------------ #------------------------------------------------------------------------------ # trim # =head2 trim($string), ltrim($string), rtrim($string) Returns the string with all leading and trailing whitespace removed. $var = trim(" my string "); # "my string" C trims B whitespace only. C trims B whitespace only. =cut sub trim { my $s = shift(); if (!defined($s)) { return undef; } $s =~ s/^\s*//u; $s =~ s/\s*$//u; return $s; } # # trim #------------------------------------------------------------------------------ #------------------------------------------------------------------------------ # ltrim, rtrim # sub ltrim { my $s = shift(); if (!defined($s)) { return undef; } $s =~ s/^\s*//u; return $s; } sub rtrim { my $s = shift(); if (!defined($s)) { return undef; } $s =~ s/\s*$//u; return $s; } # # ltrim, rtrim #------------------------------------------------------------------------------ #------------------------------------------------------------------------------ # no_space # =head2 nospace($string) Removes B whitespace characters from the given string. This includes spaces between words. $var = nospace(" Hello World! "); # "HelloWorld!" =cut sub no_space { return nospace(@_); } # alias nospace to no_space sub nospace { my $val = shift(); if (defined $val) { $val =~ s|\s+||gs; } return $val; } # # no_space #------------------------------------------------------------------------------ #------------------------------------------------------------------------------ # htmlesc # =head2 htmlesc($string) Formats a string for literal output in HTML. An undefined value is returned as an empty string. htmlesc() is very similar to CGI.pm's escapeHTML. However, there are a few differences. htmlesc() changes an undefined value to an empty string, whereas escapeHTML() returns undefs as undefs. =cut sub htmlesc { my ($val) = @_; if (defined $val) { $val =~ s|\&|&|g; $val =~ s|\"|"|g; $val =~ s|\<|<|g; $val =~ s|\>|>|g; } else { $val = ''; } return $val; } # # htmlesc #------------------------------------------------------------------------------ #------------------------------------------------------------------------------ # jsquote # =head2 jsquote($string) Escapes and quotes a string for use in JavaScript. Escapes single quotes and surrounds the string in single quotes. Returns the modified string. =cut sub jsquote { my ($str) = @_; if (!defined($str)) { return undef; } # Escape single quotes. $str =~ s|'|\\'|gs; # Break up anything that looks like a closing HTML tag. This modification # is necessary in an HTML web page. It is unnecessary but harmless if the # output is used in a JavaScript document. $str =~ s| braces If the braces option is true, surrounding braces such as [] and {} are also removed. Some examples: unquote(q|[Janis]|, braces=>1); # Janis unquote(q|{Janis}|, braces=>1); # Janis unquote(q|(Janis)|, braces=>1); # Janis =cut sub unquote { my ($val, %opts) = @_; if (defined $val) { my $found = $val =~ s|^\`(.*)\`$|$1|s or $val =~ s|^\"(.*)\"$|$1|s or $val =~ s|^\'(.*)\'$|$1|s; if ($opts{'braces'} && ! $found) { $val =~ s|^\[(.*)\]$|$1|s or $val =~ s|^\((.*)\)$|$1|s or $val =~ s|^\{(.*)\}$|$1|s; } } return $val; } # # unquote #------------------------------------------------------------------------------ #------------------------------------------------------------------------------ # repeat # =head2 repeat($string, $count) Returns the given string repeated the given number of times. The following command outputs "Fred" three times: print repeat('Fred', 3), "\n"; Note that C was created a long time based on a misunderstanding of how the perl operator 'x' works. The following command using C would perform exactly the same as the above command. print 'Fred' x 3, "\n"; Use whichever you prefer. =cut sub repeat { my ($val, $count) = @_; return ($val x int($count)); } # # repeat #------------------------------------------------------------------------------ #------------------------------------------------------------------------------ # eqq # formerly equndef # =head2 eqq($scalar1, $scalar2) Returns true if the two given values are equal. Also returns true if both are C. If only one is C, or if they are both defined but different, returns false. Here are some examples and what they return. $var = eqq('x', 'x'); # True $var = eqq('x', undef); # False $var = eqq(undef, undef); # True =cut sub eqq { my ($str1, $str2) = @_; # if both defined if ( defined($str1) && defined($str2) ) { return $str1 eq $str2 } # if neither are defined if ( (!defined($str1)) && (!defined($str2)) ) { return 1 } # only one is defined, so return false return 0; } # # eqq #------------------------------------------------------------------------------ #------------------------------------------------------------------------------ # neqq # formerly neundef # =head2 neqq($scalar1, $scalar2) The opposite of C, returns true if the two values are *not* the same. Here are some examples and what they return. $var = neqq('x', 'x'); # False $var = neqq('x', undef); # True $var = neqq(undef, undef); # False =cut sub neqq { return eqq(@_) ? 0 : 1; } # # neqq #------------------------------------------------------------------------------ #------------------------------------------------------------------------------ # ords # =head2 ords($string) Returns the given string represented as the ascii value of each character. $var = ords('Hendrix'); # {72}{101}{110}{100}{114}{105}{120} B =over 4 =item * convert_spaces=>[true|false] If convert_spaces is true (which is the default) then spaces are converted to their matching ord values. So, for example, this code: $var = ords('a b', convert_spaces=>1); # {97}{32}{98} This code returns the same thing: $var = ords('a b'); # {97}{32}{98} If convert_spaces is false, then spaces are just returned as spaces. So this code: ords('a b', convert_spaces=>0); # {97} {98} =item * alpha_nums If the alpha_nums option is false, then characters 0-9, a-z, and A-Z are not converted. For example, this code: $var = ords('a=b', alpha_nums=>0); # a{61}b =back =cut sub ords { my ($str, %opts) = @_; my (@rv, $show_chars); # default options %opts = (convert_spaces=>1, alpha_nums=>1, %opts); # get $show_chars option $show_chars = $opts{'show_chars'}; # split into individual characters @rv = split '', $str; # change elements to their unicode numbers CHAR_LOOP: foreach my $char (@rv) { # don't convert space if called so if ( (! $opts{'convert_spaces'}) && ($char =~ m|^\s$|s) ) { next CHAR_LOOP } # don't convert alphanums if (! $opts{'alpha_nums'}) { if ( $char =~ m|^[a-z0-9]$|si) { next CHAR_LOOP; } } my $rv = '{'; if ($show_chars) { $rv .= $char . ':' } $rv .= ord($char) . '}'; $char = $rv; } # return words separated by spaces return join('', @rv); } # # ords #------------------------------------------------------------------------------ #------------------------------------------------------------------------------ # deords # =head2 deords($string) Takes the output from C and returns the string that original created that output. $var = deords('{72}{101}{110}{100}{114}{105}{120}'); # 'Hendrix' =cut sub deords { my ($str) = @_; my (@tokens, $rv); $rv = ''; # get tokens @tokens = split(m|[\{\}]|s, $str); @tokens = grep {length($_)} @tokens; # build return string foreach my $token (@tokens) { $rv .= chr($token); } # return return $rv; } # # deords #------------------------------------------------------------------------------ =head2 contains($string, $substring) Checks if the string contains substring $var = contains("Hello world", "Hello"); # true $var = contains("Hello world", "llo wor"); # true $var = contains("Hello world", "QQQ"); # false # Also works with grep @arr = grep { contains("cat") } @input; =cut sub contains { my ($str, $substr) = @_; if (!defined($str)) { return undef; } if (!$substr) { $substr = $str; $str = $_; } my $ret = index($str, $substr, 0) != -1; return $ret; } =head2 startswith($string, $substring) Checks if the string starts with the characters in substring $var = startwith("Hello world", "Hello"); # true $var = startwith("Hello world", "H"); # true $var = startwith("Hello world", "Q"); # false # Also works with grep @arr = grep { startswith("X") } @input; =cut sub startswith { my ($str, $substr) = @_; if (!defined($str)) { return undef; } if (!$substr) { $substr = $str; $str = $_; } my $ret = index($str, $substr, 0) == 0; return $ret; } =head2 endswith($string, $substring) Checks if the string ends with the characters in substring $var = endswith("Hello world", "world"); # true $var = endswith("Hello world", "d"); # true $var = endswith("Hello world", "QQQ"); # false # Also works with grep @arr = grep { endswith("z") } @input; =cut sub endswith { my ($str, $substr) = @_; if (!defined($str)) { return undef; } if (!$substr) { $substr = $str; $str = $_; } my $len = length($substr); my $start = length($str) - $len; my $ret = index($str, $substr, $start) != -1; return $ret; } #------------------------------------------------------------------------------ # crunchlines # =head2 crunchlines($string) Compacts contiguous newlines into single newlines. Whitespace between newlines is ignored, so that two newlines separated by whitespace is compacted down to a single newline. $var = crunchlines("x\n\n\nx"); # "x\nx"; =cut sub crunchlines { my ($str) = @_; if (!defined($str)) { return undef; } while($str =~ s|\n[ \t]*\n|\n|gs) {} $str =~ s|^\n||s; $str =~ s|\n$||s; return $str; } # # crunchlines #------------------------------------------------------------------------------ =head2 sanitize($string, $separator = "_") Sanitize all non alpha-numeric characters in a string to underscores. This is useful to take a URL, or filename, or text description and know you can use it safely in a URL or a filename. B This will remove any trailing or leading '_' on the string $var = sanitize("http://www.google.com/") # http_www_google_com $var = sanitize("foo_bar()"; # foo_bar $var = sanitize("/path/to/file.txt"); # path_to_file_txt $var = sanitize("Big yellow bird!", "."); # Big.yellow.bird =cut sub sanitize { my $str = shift(); my $sep = shift() // "_"; if (!defined($str)) { return undef; } # Convert multiple non-word sequences to the separator $str =~ s/[\W_]+/$sep/g; # The separator is a literal character so we quotemeta it $sep = quotemeta($sep); # Remove any separators at the beginning and end $str =~ s/\A$sep+//; $str =~ s/$sep+\z//; return $str; } ########################################################################### =head2 file_get_contents($string, $boolean) Read an entire file from disk into a string. Returns undef if the file cannot be read for any reason. Can also return the file as an array of lines. $str = file_get_contents("/tmp/file.txt"); # Return a string @lines = file_get_contents("/tmp/file.txt", 1); # Return an array B If you opt to return an array, carriage returns and line feeds are removed from the end of each line. B File is read in B mode, unless C<$FGC_MODE> is set to an appropriate encoding. =cut sub file_get_contents { my ($file, $ret_array) = @_; open (my $fh, "<", $file) or return undef; binmode($fh, ":encoding($FGC_MODE)"); if ($ret_array) { my @ret; while (my $line = readline($fh)) { $line =~ s/[\r\n]*$//; # Remove CR/LF push(@ret, $line); } return @ret; } else { my $ret = ''; while (my $line = readline($fh)) { $ret .= $line; } return $ret; } } # return true 1; __END__ =head1 COPYRIGHT AND LICENSE Copyright (c) 2012-2016 by Miko O'Sullivan. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. This software comes with B of any kind. =head1 AUTHORS Miko O'Sullivan Scott Baker =cut #------------------------------------------------------------------------------ # module info # This info is used by a home-grown CPAN builder. Please leave it as it is. # { // include in CPAN distribution include : 1, // test scripts test_scripts : { 'Util/tests/test.pl' : 1, }, } # # module info #------------------------------------------------------------------------------