├── .travis.yml ├── example └── example.go ├── .gitignore ├── pad.go ├── utf8 ├── pad.go └── pad_test.go ├── .github └── ISSUE_TEMPLATE │ ├── feature_request.md │ └── bug_report.md ├── README.md ├── LICENSE.txt └── pad_test.go /.travis.yml: -------------------------------------------------------------------------------- 1 | language: go 2 | 3 | -------------------------------------------------------------------------------- /example/example.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/willf/pad" 7 | padUtf8 "github.com/willf/pad/utf8" 8 | ) 9 | 10 | func main() { 11 | fmt.Println(pad.Right("Hello", 20, "!")) 12 | fmt.Println(padUtf8.Left("Exit now", 20, "→")) 13 | } 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | *.prof 25 | -------------------------------------------------------------------------------- /pad.go: -------------------------------------------------------------------------------- 1 | /* 2 | Package pad provides left-padding functionality 3 | 4 | 5 | */ 6 | package pad 7 | 8 | import "strings" 9 | 10 | func times(str string, n int) string { 11 | if n <= 0 { 12 | return "" 13 | } 14 | return strings.Repeat(str, n) 15 | } 16 | 17 | // Left left-pads the string with pad up to len runes 18 | // len may be exceeded if 19 | func Left(str string, length int, pad string) string { 20 | return times(pad, length-len(str)) + str 21 | } 22 | 23 | // Right right-pads the string with pad up to len runes 24 | func Right(str string, length int, pad string) string { 25 | return str + times(pad, length-len(str)) 26 | } 27 | -------------------------------------------------------------------------------- /utf8/pad.go: -------------------------------------------------------------------------------- 1 | /* 2 | Package utf8 provides left-padding and right-padding for utf8 strings 3 | 4 | 5 | */ 6 | package utf8 7 | 8 | import "unicode/utf8" 9 | 10 | func times(str string, n int) (out string) { 11 | for i := 0; i < n; i++ { 12 | out += str 13 | } 14 | return 15 | } 16 | 17 | // Left left-pads the string with pad up to len runes 18 | // len may be exceeded if 19 | func Left(str string, len int, pad string) string { 20 | return times(pad, len-utf8.RuneCountInString(str)) + str 21 | } 22 | 23 | // Right right-pads the string with pad up to len runes 24 | func Right(str string, len int, pad string) string { 25 | return str + times(pad, len-utf8.RuneCountInString(str)) 26 | } 27 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 1. Go to '...' 16 | 2. Click on '....' 17 | 3. Scroll down to '....' 18 | 4. See error 19 | 20 | **Expected behavior** 21 | A clear and concise description of what you expected to happen. 22 | 23 | **Screenshots** 24 | If applicable, add screenshots to help explain your problem. 25 | 26 | **Desktop (please complete the following information):** 27 | - OS: [e.g. iOS] 28 | - Browser [e.g. chrome, safari] 29 | - Version [e.g. 22] 30 | 31 | **Smartphone (please complete the following information):** 32 | - Device: [e.g. iPhone6] 33 | - OS: [e.g. iOS8.1] 34 | - Browser [e.g. stock browser, safari] 35 | - Version [e.g. 22] 36 | 37 | **Additional context** 38 | Add any other context about the problem here. 39 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | pad 2 | ------------- 3 | 4 | [![Join the chat at https://gitter.im/willf/pad](https://badges.gitter.im/willf/pad.svg)](https://gitter.im/willf/pad?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) 5 | 6 | [![Build Status](https://travis-ci.org/willf/pad.svg?branch=master)](https://travis-ci.org/willf/pad) 7 | 8 | A golang implementation of the [left-pad javascript library](https://www.npmjs.com/package/left-pad) 9 | 10 | I was inspired by [Stew](https://twitter.com/StewOConnor)'s [`left-cats`](https://github.com/stew/left-cats), who was inspired by [this article](http://arstechnica.com/information-technology/2016/03/rage-quit-coder-unpublished-17-lines-of-javascript-and-broke-the-internet/), to port this to Go. 11 | 12 | This implementation will let you pad byte-strings and UTF-8 encoded strings 13 | 14 | example usage: 15 | 16 | ```go 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | 22 | "github.com/willf/pad" 23 | padUtf8 "github.com/willf/pad/utf8" 24 | ) 25 | 26 | func main() { 27 | fmt.Println(pad.Right("Hello", 20, "!")) 28 | fmt.Println(padUtf8.Left("Exit now", 20, "→")) 29 | } 30 | ``` 31 | 32 | ```bash 33 | > go run example.go 34 | Hello!!!!!!!!!!!!!!! 35 | →→→→→→→→→→→→Exit now 36 | ``` 37 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016 Will Fitzgerald. All rights reserved. 2 | 3 | Redistribution and use in source and binary forms, with or without 4 | modification, are permitted provided that the following conditions are 5 | met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above 10 | copyright notice, this list of conditions and the following disclaimer 11 | in the documentation and/or other materials provided with the 12 | distribution. 13 | 14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 15 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 16 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 17 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 18 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 19 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 20 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | -------------------------------------------------------------------------------- /pad_test.go: -------------------------------------------------------------------------------- 1 | package pad 2 | 3 | import ( 4 | "testing" 5 | "testing/quick" 6 | ) 7 | 8 | func TestLeftEqualWithSameLength(t *testing.T) { 9 | f := func(a string, pad string) bool { 10 | slen := len(a) 11 | padded := Left(a, slen, pad) 12 | return padded == a 13 | } 14 | if err := quick.Check(f, nil); err != nil { 15 | t.Error(err) 16 | } 17 | } 18 | 19 | func TestRightEqualWithSameLength(t *testing.T) { 20 | f := func(a string, pad string) bool { 21 | slen := len(a) 22 | padded := Right(a, slen, pad) 23 | return padded == a 24 | } 25 | if err := quick.Check(f, nil); err != nil { 26 | t.Error(err) 27 | } 28 | } 29 | 30 | func TestLeftEqualWithShorterLength(t *testing.T) { 31 | f := func(a string, pad string) bool { 32 | slen := len(a) 33 | padded := Left(a, slen-3, pad) 34 | return padded == a 35 | } 36 | if err := quick.Check(f, nil); err != nil { 37 | t.Error(err) 38 | } 39 | } 40 | 41 | func TestRightEqualWithShorterLength(t *testing.T) { 42 | f := func(a string, pad string) bool { 43 | slen := len(a) 44 | padded := Right(a, slen-3, pad) 45 | return padded == a 46 | } 47 | if err := quick.Check(f, nil); err != nil { 48 | t.Error(err) 49 | } 50 | } 51 | 52 | func TestLeftEqualWithGreaterLength(t *testing.T) { 53 | f := func(a string, pad string) bool { 54 | slen := len(a) + 3 55 | padded := Left(a, slen, pad) 56 | return padded == times(pad, 3)+a 57 | } 58 | if err := quick.Check(f, nil); err != nil { 59 | t.Error(err) 60 | } 61 | } 62 | 63 | func TestRightEqualWithGreaterLength(t *testing.T) { 64 | f := func(a string, pad string) bool { 65 | slen := len(a) + 3 66 | padded := Right(a, slen, pad) 67 | return padded == a+times(pad, 3) 68 | } 69 | if err := quick.Check(f, nil); err != nil { 70 | t.Error(err) 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /utf8/pad_test.go: -------------------------------------------------------------------------------- 1 | package utf8 2 | 3 | import ( 4 | "testing" 5 | "testing/quick" 6 | "unicode/utf8" 7 | ) 8 | 9 | func TestLeftEqualWithSameLength(t *testing.T) { 10 | f := func(a string, pad string) bool { 11 | slen := utf8.RuneCountInString(a) 12 | padded := Left(a, slen, pad) 13 | return padded == a 14 | } 15 | if err := quick.Check(f, nil); err != nil { 16 | t.Error(err) 17 | } 18 | } 19 | 20 | func TestRightEqualWithSameLength(t *testing.T) { 21 | f := func(a string, pad string) bool { 22 | slen := utf8.RuneCountInString(a) 23 | padded := Right(a, slen, pad) 24 | return padded == a 25 | } 26 | if err := quick.Check(f, nil); err != nil { 27 | t.Error(err) 28 | } 29 | } 30 | 31 | func TestLeftEqualWithShorterLength(t *testing.T) { 32 | f := func(a string, pad string) bool { 33 | slen := utf8.RuneCountInString(a) 34 | padded := Left(a, slen-3, pad) 35 | return padded == a 36 | } 37 | if err := quick.Check(f, nil); err != nil { 38 | t.Error(err) 39 | } 40 | } 41 | 42 | func TestRightEqualWithShorterLength(t *testing.T) { 43 | f := func(a string, pad string) bool { 44 | slen := utf8.RuneCountInString(a) 45 | padded := Right(a, slen-3, pad) 46 | return padded == a 47 | } 48 | if err := quick.Check(f, nil); err != nil { 49 | t.Error(err) 50 | } 51 | } 52 | 53 | func TestLeftEqualWithGreaterLength(t *testing.T) { 54 | f := func(a string, pad string) bool { 55 | slen := utf8.RuneCountInString(a) + 3 56 | padded := Left(a, slen, pad) 57 | return padded == times(pad, 3)+a 58 | } 59 | if err := quick.Check(f, nil); err != nil { 60 | t.Error(err) 61 | } 62 | } 63 | 64 | func TestRightEqualWithGreaterLength(t *testing.T) { 65 | f := func(a string, pad string) bool { 66 | slen := utf8.RuneCountInString(a) + 3 67 | padded := Right(a, slen, pad) 68 | return padded == a+times(pad, 3) 69 | } 70 | if err := quick.Check(f, nil); err != nil { 71 | t.Error(err) 72 | } 73 | } 74 | --------------------------------------------------------------------------------