Skip to content

To_Title is corrected #372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions src/stdlib_ascii.f90
Original file line number Diff line number Diff line change
Expand Up @@ -277,23 +277,30 @@ end function to_upper
pure function to_title(string) result(title_string)
character(len=*), intent(in) :: string
character(len=len(string)) :: title_string
logical:: capitalize_flag
integer :: i, n

n = len(string)
capitalize_flag = .TRUE.
do i = 1, len(string)
if (is_alphanum(string(i:i))) then
title_string(i:i) = char_to_upper(string(i:i))
n = i
exit

if(capitalize_flag) then
if (is_alphanum(string(i:i))) then
title_string(i:i) = char_to_upper(string(i:i))
capitalize_flag = .FALSE.
else
title_string(i:i) = char_to_lower(string(i:i))
end if
else
title_string(i:i) = string(i:i)
if(string(i:i)==" ") then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be more general than just spaces, every non-word boundary might trigger capitalization, consider:
to_title(".this.sentence.is.separated.with.dots."), would we expect .This.sentence.is.separated.with.dots. or .This.Sentence.Is.Separated.With.Dots. for a titlecase return value?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm! I am not sure what to expect in such a case. My favor would go to capitalize each word. What do other languages?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked for python and realised that every character which is not an alphabet triggers capitalization including integers. Python mentions that even apostrophe(') triggers this capitalization which is undesirable. May be we can take an optional set as the argument from the user which will consist of characters the user wants to the trigger capitalization for..

title_string(i:i) = char_to_lower(string(i:i))
capitalize_flag = .TRUE.
else
title_string(i:i) = char_to_lower(string(i:i))
end if
end if
end do

do i = n + 1, len(string)
title_string(i:i) = char_to_lower(string(i:i))
end do

end function to_title

!> Reverse the character order in the input character variable
Expand Down
2 changes: 1 addition & 1 deletion src/tests/ascii/test_ascii.f90
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ subroutine test_to_title_string
call check(trim(dlc) == "Title")

dlc = to_title(" s P a C e D !")
call check(dlc == " S p a c e d !")
call check(dlc == " S P A C E D !")

dlc = to_title("1st, 2nd, 3rd")
call check(dlc == "1st, 2nd, 3rd")
Expand Down
2 changes: 1 addition & 1 deletion src/tests/string/test_string_functions.f90
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ end subroutine test_to_upper_string
subroutine test_to_title_string
type(string_type) :: test_string, compare_string
test_string = "_#To tiTlE !$%-az09AZ"
compare_string = "_#To title !$%-az09az"
compare_string = "_#To Title !$%-Az09az"

call check(to_title(test_string) == compare_string)

Expand Down