github.com/synesissoftware/ANGoLS@v0.0.0-20190330004400-955d82dbf73b/chomps.go (about)

     1  /* /////////////////////////////////////////////////////////////////////////
     2   * File:        chomps.go
     3   *
     4   * Purpose:     string chomp functions
     5   *
     6   * Created:     30th March 2019
     7   * Updated:     30th March 2019
     8   *
     9   * Home:        http://github.com/synesissoftware/ANGOLS
    10   *
    11   * Copyright (c) 2019, Matthew Wilson and Synesis Software
    12   * All rights reserved.
    13   *
    14   * Redistribution and use in source and binary forms, with or without
    15   * modification, are permitted provided that the following conditions are
    16   * met:
    17   *
    18   * - Redistributions of source code must retain the above copyright notice,
    19   *   this list of conditions and the following disclaimer.
    20   * - Redistributions in binary form must reproduce the above copyright
    21   *   notice, this list of conditions and the following disclaimer in the
    22   *   documentation and/or other materials provided with the distribution.
    23   * - Neither the names of Matthew Wilson, Synesis Software nor
    24   *   the names of any contributors may be used to endorse or promote products
    25   *   derived from this software without specific prior written permission.
    26   *
    27   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
    28   * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
    29   * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    30   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    31   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    32   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    33   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    34   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    35   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    36   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    37   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    38   *
    39   * ////////////////////////////////////////////////////////////////////// */
    40  
    41  package angols
    42  
    43  // StringChomp() takes a single string and returns a chomped version of it,
    44  // where chomping removes a single trailing '\n' character, a single
    45  // trailing '\r' character, or a single trailing sequence of "\r\n"
    46  func StringChomp(s string) (string) {
    47  
    48  	switch l := len(s); l {
    49  
    50  	case 0:
    51  
    52  		break
    53  	default:
    54  
    55  		if '\r' == s[l - 2] && '\n' == s[l - 1] {
    56  
    57  			s = s[0:l - 2]
    58  			break
    59  		}
    60  
    61  		fallthrough
    62  	case 1:
    63  
    64  		switch s[l - 1] {
    65  
    66  		case '\r', '\n':
    67  
    68  			s = s[0:l - 1]
    69  		}
    70  	}
    71  
    72  	return s
    73  }
    74  
    75  // StringChompAll() takes a single string and returns a fully/repeatedly
    76  // chomped version of, where full/repeated chomping removes all trailing
    77  // '\r' and/or '\n' characters
    78  func StringChompAll(s string) (string) {
    79  
    80  out:
    81  	for l := len(s); l > 0; {
    82  
    83  		switch s[l - 1] {
    84  
    85  		case '\r', '\n':
    86  
    87  			l--
    88  			s = s[0:l]
    89  		default:
    90  
    91  			break out
    92  		}
    93  	}
    94  
    95  	return s
    96  }
    97  
    98  /* ///////////////////////////// end of file //////////////////////////// */
    99  
   100