-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement strip and chomp as supplement to trim
- Loading branch information
Showing
8 changed files
with
317 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
--- | ||
title: string handling | ||
--- | ||
|
||
# The `stdlib_strings` module | ||
|
||
[TOC] | ||
|
||
## Introduction | ||
|
||
The `stdlib_strings` module provides basic string handling and manipulation routines. | ||
|
||
|
||
## Procedures and methods provided | ||
|
||
|
||
<!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --> | ||
### `strip` | ||
|
||
#### Description | ||
|
||
Remove leading and trailing whitespace characters. | ||
|
||
#### Syntax | ||
|
||
`string = [[stdlib_strings(module):strip(interface)]] (string)` | ||
|
||
#### Status | ||
|
||
Experimental | ||
|
||
#### Class | ||
|
||
Pure function. | ||
|
||
#### Argument | ||
|
||
- `string`: Character scalar or [[stdlib_string_type(module):string_type(type)]]. | ||
This argument is intent(in). | ||
|
||
#### Result value | ||
|
||
The result is of the same type as `string`. | ||
|
||
#### Example | ||
|
||
```fortran | ||
program demo | ||
use stdlib_ascii, only : TAB, VT, NUL, LF, CR, FF | ||
use stdlib_strings, only : strip | ||
implicit none | ||
print'(a)', strip(" hello ") ! "hello" | ||
print'(a)', strip(TAB//"goodbye"//CR//LF) ! "goodbye" | ||
print'(a)', strip(" "//TAB//LF//VT//FF//CR) ! "" | ||
print'(a)', strip(" ! ")//"!" ! "!!" | ||
print'(a)', strip("Hello") ! "Hello" | ||
end program demo | ||
``` | ||
|
||
|
||
<!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --> | ||
### `chomp` | ||
|
||
#### Description | ||
|
||
Remove trailing characters in *set* from *string*. | ||
If no character *set* is provided trailing whitespace is removed. | ||
|
||
#### Syntax | ||
|
||
`string = [[stdlib_strings(module):chomp(interface)]] (string[, set])` | ||
|
||
#### Status | ||
|
||
Experimental | ||
|
||
#### Class | ||
|
||
Pure function. | ||
|
||
#### Argument | ||
|
||
- `string`: Character scalar or [[stdlib_string_type(module):string_type(type)]]. | ||
This argument is intent(in). | ||
- `set`: Character scalar or [[stdlib_string_type(module):string_type(type)]]. | ||
This argument is intent(in). | ||
|
||
#### Result value | ||
|
||
The result is of the same type as `string`. | ||
|
||
#### Example | ||
|
||
```fortran | ||
program demo | ||
use stdlib_ascii, only : TAB, VT, NUL, LF, CR, FF | ||
use stdlib_strings, only : chomp | ||
implicit none | ||
print'(a)', chomp(" hello ") ! " hello" | ||
print'(a)', chomp(TAB//"goodbye"//CR//LF) ! "\tgoodbye" | ||
print'(a)', chomp(" "//TAB//LF//VT//FF//CR) ! "" | ||
print'(a)', chomp(" ! ")//"!" ! " !!" | ||
print'(a)', chomp("Hello") ! "Hello" | ||
print'(a)', chomp("hello", set="lo") ! "he" | ||
end program demo | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
! SPDX-Identifier: MIT | ||
|
||
!> This module implements basic string handling routines. | ||
!> | ||
!> The specification of this module is available [here](../page/specs/stdlib_strings.html). | ||
module stdlib_strings | ||
use stdlib_ascii, only : whitespace | ||
use stdlib_string_type, only : string_type, char | ||
implicit none | ||
private | ||
|
||
public :: strip, chomp | ||
|
||
|
||
!> Remove leading and trailing whitespace characters. | ||
!> | ||
!> Version: experimental | ||
interface strip | ||
module procedure :: strip_string | ||
module procedure :: strip_char | ||
end interface strip | ||
|
||
!> Remove trailing characters in set from string. | ||
!> If no character set is provided trailing whitespace is removed. | ||
!> | ||
!> Version: experimental | ||
interface chomp | ||
module procedure :: chomp_string | ||
module procedure :: chomp_char | ||
module procedure :: chomp_string_string | ||
module procedure :: chomp_char_string | ||
module procedure :: chomp_string_char | ||
module procedure :: chomp_char_char | ||
end interface chomp | ||
|
||
|
||
contains | ||
|
||
|
||
!> Remove leading and trailing whitespace characters. | ||
pure function strip_string(string) result(stripped_string) | ||
! Avoid polluting the module scope and use the assignment only in this scope | ||
use stdlib_string_type, only : assignment(=) | ||
type(string_type), intent(in) :: string | ||
type(string_type) :: stripped_string | ||
|
||
stripped_string = strip(char(string)) | ||
end function strip_string | ||
|
||
!> Remove leading and trailing whitespace characters. | ||
pure function strip_char(string) result(stripped_string) | ||
character(len=*), intent(in) :: string | ||
character(len=:), allocatable :: stripped_string | ||
integer :: first, last | ||
|
||
first = verify(string, whitespace) | ||
if (first == 0) then | ||
stripped_string = "" | ||
else | ||
last = verify(string, whitespace, back=.true.) | ||
stripped_string = string(first:last) | ||
end if | ||
|
||
end function strip_char | ||
|
||
|
||
!> Remove trailing characters in set from string. | ||
!> Default character set variant where trailing whitespace is removed. | ||
pure function chomp_string(string) result(chomped_string) | ||
! Avoid polluting the module scope and use the assignment only in this scope | ||
use stdlib_string_type, only : assignment(=) | ||
type(string_type), intent(in) :: string | ||
type(string_type) :: chomped_string | ||
|
||
chomped_string = chomp(char(string), whitespace) | ||
end function chomp_string | ||
|
||
!> Remove trailing characters in set from string. | ||
!> Default character set variant where trailing whitespace is removed. | ||
pure function chomp_char(string) result(chomped_string) | ||
character(len=*), intent(in) :: string | ||
character(len=:), allocatable :: chomped_string | ||
|
||
chomped_string = chomp(string, whitespace) | ||
end function chomp_char | ||
|
||
!> Remove trailing characters in set from string. | ||
pure function chomp_string_string(string, set) result(chomped_string) | ||
! Avoid polluting the module scope and use the assignment only in this scope | ||
use stdlib_string_type, only : assignment(=) | ||
type(string_type), intent(in) :: string | ||
type(string_type), intent(in) :: set | ||
type(string_type) :: chomped_string | ||
|
||
chomped_string = chomp(char(string), char(set)) | ||
end function chomp_string_string | ||
|
||
!> Remove trailing characters in set from string. | ||
pure function chomp_string_char(string, set) result(chomped_string) | ||
! Avoid polluting the module scope and use the assignment only in this scope | ||
use stdlib_string_type, only : assignment(=) | ||
type(string_type), intent(in) :: string | ||
character(len=*), intent(in) :: set | ||
type(string_type) :: chomped_string | ||
|
||
chomped_string = chomp(char(string), set) | ||
end function chomp_string_char | ||
|
||
!> Remove trailing characters in set from string. | ||
pure function chomp_char_string(string, set) result(chomped_string) | ||
character(len=*), intent(in) :: string | ||
type(string_type), intent(in) :: set | ||
character(len=:), allocatable :: chomped_string | ||
|
||
chomped_string = chomp(string, char(set)) | ||
end function chomp_char_string | ||
|
||
!> Remove trailing characters in set from string. | ||
pure function chomp_char_char(string, set) result(chomped_string) | ||
character(len=*), intent(in) :: string | ||
character(len=*), intent(in) :: set | ||
character(len=:), allocatable :: chomped_string | ||
integer :: last | ||
|
||
last = verify(string, set, back=.true.) | ||
chomped_string = string(1:last) | ||
|
||
end function chomp_char_char | ||
|
||
|
||
end module stdlib_strings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
! SPDX-Identifier: MIT | ||
module test_trim | ||
use stdlib_ascii, only : TAB, VT, NUL, LF, CR, FF | ||
use stdlib_error, only : check | ||
use stdlib_strings, only : strip, chomp | ||
use stdlib_string_type, only : string_type, operator(==), operator(//) | ||
implicit none | ||
|
||
contains | ||
|
||
subroutine test_strip_char | ||
call check(strip(" hello ") == "hello") | ||
call check(strip(TAB//"goodbye"//CR//LF) == "goodbye") | ||
call check(strip(NUL//TAB//LF//VT//FF//CR) == NUL) | ||
call check(strip(" "//TAB//LF//VT//FF//CR) == "") | ||
call check(strip(" ! ")//"!" == "!!") | ||
call check(strip("Hello") == "Hello") | ||
end subroutine test_strip_char | ||
|
||
subroutine test_strip_string | ||
call check(strip(string_type(" hello ")) == "hello") | ||
call check(strip(string_type(TAB//"goodbye"//CR//LF)) == "goodbye") | ||
call check(strip(string_type(NUL//TAB//LF//VT//FF//CR)) == NUL) | ||
call check(strip(string_type(" "//TAB//LF//VT//FF//CR)) == "") | ||
call check(strip(string_type(" ! "))//"!" == "!!") | ||
call check(strip(string_type("Hello")) == "Hello") | ||
end subroutine test_strip_string | ||
|
||
subroutine test_chomp_char | ||
call check(chomp("hello") == "hello") | ||
call check(chomp("hello"//LF) == "hello") | ||
call check(chomp("hello"//CR//LF) == "hello") | ||
call check(chomp("hello"//LF//CR) == "hello") | ||
call check(chomp("hello"//CR) == "hello") | ||
call check(chomp("hello "//LF//" there") == "hello "//LF//" there") | ||
call check(chomp("hello", set="lo") == "he") | ||
call check(chomp("hello"//CR//LF//CR//LF) == "hello") | ||
call check(chomp("hello"//CR//LF//CR//CR//LF) == "hello") | ||
call check(chomp(NUL//TAB//LF//VT//FF//CR) == NUL) | ||
call check(chomp(" "//TAB//LF//VT//FF//CR) == "") | ||
call check(chomp(" ! ")//"!" == " !!") | ||
end subroutine test_chomp_char | ||
|
||
subroutine test_chomp_string | ||
call check(chomp(string_type("hello")) == "hello") | ||
call check(chomp(string_type("hello"//LF)) == "hello") | ||
call check(chomp(string_type("hello"//CR//LF)) == "hello") | ||
call check(chomp(string_type("hello"//LF//CR)) == "hello") | ||
call check(chomp(string_type("hello"//CR)) == "hello") | ||
call check(chomp(string_type("hello "//LF//" there")) == "hello "//LF//" there") | ||
call check(chomp(string_type("hello"), set="lo") == "he") | ||
call check(chomp("hello", set=string_type("lo")) == "he") | ||
call check(chomp(string_type("hello"), set=string_type("lo")) == "he") | ||
call check(chomp(string_type("hello"//CR//LF//CR//LF)) == "hello") | ||
call check(chomp(string_type("hello"//CR//LF//CR//CR//LF)) == "hello") | ||
call check(chomp(string_type(NUL//TAB//LF//VT//FF//CR)) == NUL) | ||
call check(chomp(string_type(" "//TAB//LF//VT//FF//CR)) == "") | ||
call check(chomp(string_type(" ! "))//"!" == " !!") | ||
end subroutine test_chomp_string | ||
|
||
end module test_trim | ||
|
||
program tester | ||
use test_trim | ||
implicit none | ||
|
||
call test_strip_char | ||
call test_strip_string | ||
call test_chomp_char | ||
call test_chomp_string | ||
|
||
end program tester |