├── .gitignore ├── Cargo.toml ├── README.md ├── .travis.yml ├── LICENSE └── src └── lib.rs /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | Cargo.lock 3 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "left-pad" 3 | version = "1.0.1" 4 | authors = ["Felix Rath "] 5 | description = "Provides left-padding for strings." 6 | keywords = ["padding", "pad"] 7 | repository = "https://github.com/futile/leftpad-rs.git" 8 | homepage = "https://futile.github.io/leftpad-rs" 9 | documentation = "https://futile.github.io/leftpad-rs" 10 | license = "MIT" 11 | readme = "README.md" 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # leftpad-rs 2 | 3 | ##### Note: Rust's builtin `format!` can achieve the same functionality: https://doc.rust-lang.org/std/fmt/index.html#fillalignment 4 | 5 | This crate provides generic left-padding functions for strings, including both `&str` and `String`. 6 | 7 | Import with `extern crate left_pad;`. 8 | 9 | ## [Link to the documentation](https://futile.github.io/leftpad-rs/left_pad/index.html) 10 | 11 | Usage example: 12 | 13 | ```rust 14 | use left_pad::{leftpad, leftpad_with}; 15 | 16 | assert_eq!(leftpad("blubb", 7), " blubb"); 17 | assert_eq!(leftpad_with("blubb", 7, '.'), "..blubb"); 18 | 19 | let s: String = "blubb".to_owned(); 20 | assert_eq!(leftpad(s, 7), " blubb"); 21 | ``` 22 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: rust 2 | sudo: false 3 | rust: 4 | - beta 5 | before_script: 6 | - pip install 'travis-cargo<0.2' --user && export PATH=$HOME/.local/bin:$PATH 7 | script: 8 | - | 9 | travis-cargo build && 10 | travis-cargo test && 11 | travis-cargo bench && 12 | travis-cargo doc 13 | after_success: 14 | - travis-cargo --only beta doc-upload 15 | env: 16 | global: 17 | secure: de3SM4CfQg+TGcvyvv+zEYlFitdjdBQYZtgWsmSg4h2av7oDwFewrMnss3ml6IUoJcU47BKkWDHCtoBHI/FX93LRfL8K0c6DCgqLfUCHVJyKsXd19p2OipSGaQsBeolJeGHyWqF1pjC7pJTvClnEQGlLbMMYn4siE/c/QIbhVdgfyugk8qBLJN6ezpn5/90V+iORmnu770Rcw3NZKgS021zIrHdSYz8BBe+X73yFm/Xrml60fsvJzbL+zCQed3yXBnwoMx61WqODmqfzcOQ8QZwXJ0TmEb9XvveJ8X61cuRiF2kLTABgoGiJuQLrsinQdSt0JRusM4qqLx+0p0RhbsQ/W5s9TKpb1nsHpkGEhjxNkPa9H9h4498981mcQi9+sI+VHeGDBBFAS1WTAWRh3TKCKYeNVkZFO5x9UMst6wFW5Di+QHrLO/K7vm0Rqjd36HSJpkUA4jr/4pbJhvjJw60Mnw66ql0d3TQScxZMJKEvfkm7oZYta+BAtHnTPl3BxARP6M+wBbMBRGYR2OvYqAo27UA2XqgZS7TVxlc9qbEwPE6ZFt1PsLI/dPxUZmJJXIC9Wpqli3dKvq7AltSd2ntU5jiCFapFBzhJ0x+t+zYj4R4SwezS+EPDhUVub02CevV+S79ROpEKVGaeX2VNGtPZawLyR1xJ7ejVFGoV01g= 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 The leftpad-rs developers 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | /*! 2 | This crate provides left-padding for strings (including both `&str` and `String`). 3 | 4 | Import with `extern crate left_pad;`. 5 | 6 | Usage example: 7 | 8 | ``` 9 | use left_pad::{leftpad, leftpad_with}; 10 | 11 | assert_eq!(leftpad("blübb", 7), " blübb"); 12 | assert_eq!(leftpad_with("blübb", 7, '→'), "→→blübb"); 13 | 14 | let s: String = "blübb".to_owned(); 15 | assert_eq!(leftpad(s, 7), " blübb"); 16 | ``` 17 | */ 18 | 19 | #![deny(missing_docs)] 20 | 21 | use std::borrow::Cow; 22 | use std::borrow::Borrow; 23 | 24 | /// Pads a string to the given number of chars by inserting the character `pad_char` from the left. 25 | /// 26 | /// If the given string has more than or exactly the desired number of codepoints, it will be 27 | /// returned as-is. 28 | /// 29 | /// Codepoints are not graphemes, so the result might always not be what a human would expect. 30 | /// 31 | /// # Examples 32 | /// 33 | /// ``` 34 | /// use left_pad::leftpad_with; 35 | /// 36 | /// assert_eq!(leftpad_with("blübb", 7, ' '), " blübb"); 37 | /// assert_eq!(leftpad_with("blübb", 7, '→'), "→→blübb"); 38 | /// 39 | /// assert_eq!(leftpad_with("blübb", 5, ' '), "blübb"); 40 | /// assert_eq!(leftpad_with("blübb", 3, ' '), "blübb"); 41 | /// 42 | /// assert_eq!(leftpad_with("čömbiñiñg märks", 22, ' '), " čömbiñiñg märks"); 43 | /// ``` 44 | pub fn leftpad_with<'a, S>(string: S, codepoints: usize, pad_char: char) -> Cow<'a, str> 45 | where S: Into> 46 | { 47 | let cow = string.into(); 48 | 49 | let cow_codepoints = cow.chars().count(); 50 | if codepoints <= cow_codepoints { 51 | return cow; 52 | } 53 | 54 | let to_pad = codepoints - cow_codepoints; 55 | let mut padded = String::with_capacity(cow.len() + to_pad); 56 | 57 | for _ in 0..to_pad { 58 | padded.push(pad_char); 59 | } 60 | 61 | padded.push_str(cow.borrow()); 62 | 63 | padded.into() 64 | } 65 | 66 | /// Pads a string to the given number of chars by inserting spaces from the left. 67 | /// 68 | /// If the given string has more than or exactly the desired number of codepoints, it will be 69 | /// returned as-is. 70 | /// 71 | /// Codepoints are not graphemes, so the result might not always be what a human would expect. 72 | /// 73 | /// This function is equal to calling `leftpad_with(string, codepoints, ' ')`. 74 | /// 75 | /// # Examples 76 | /// 77 | /// ``` 78 | /// use left_pad::{leftpad,leftpad_with}; 79 | /// 80 | /// assert_eq!(leftpad("blübb", 7), " blübb"); 81 | /// 82 | /// assert_eq!(leftpad("blübb", 5), "blübb"); 83 | /// assert_eq!(leftpad("blübb", 3), "blübb"); 84 | /// 85 | /// assert_eq!(leftpad("blübb", 7), leftpad_with("blübb", 7, ' ')); 86 | /// 87 | /// assert_eq!(leftpad("čömbiñiñg märks", 22), " čömbiñiñg märks"); 88 | /// ``` 89 | pub fn leftpad<'a, S>(string: S, codepoints: usize) -> Cow<'a, str> 90 | where S: Into> 91 | { 92 | leftpad_with(string, codepoints, ' ') 93 | } 94 | --------------------------------------------------------------------------------