fortrangoingonforty/ferp / b43bcce

Browse files

Add pattern_len() function for null-terminated pattern handling

Fortran's len_trim() returns 0 for whitespace-only strings, which breaks
patterns like " " (two spaces). This function respects null terminators
to track exact pattern length, falling back to full string length when
no terminator is present.
Authored by mfwolffe <wolffemf@dukes.jmu.edu>
SHA
b43bcce5c8d8e2737ce3ed36e29bbdc3c7682c92
Parents
0d61ac5
Tree
423e3dd

1 changed file

StatusFile+-
M src/ferp_kinds.f90 30 0
src/ferp_kinds.f90modified
@@ -7,6 +7,7 @@ module ferp_kinds
77
 
88
   public :: i32, i64, dp
99
   public :: initial_line_len, max_line_len, max_path_len, max_pattern_len
10
+  public :: pattern_len
1011
 
1112
   !> Integer kinds
1213
   integer, parameter :: i32 = int32
@@ -21,4 +22,33 @@ module ferp_kinds
2122
   integer, parameter :: max_path_len = 4096
2223
   integer, parameter :: max_pattern_len = 4096
2324
 
25
+contains
26
+
27
+  pure function pattern_len(pattern) result(plen)
28
+    !> Get the true length of a pattern, respecting null terminator if present
29
+    !> This allows whitespace-only patterns like "  " to be handled correctly
30
+    !> If no null terminator is found, returns the full string length (not len_trim)
31
+    !> to properly handle whitespace-only patterns passed with explicit length
32
+    character(len=*), intent(in) :: pattern
33
+    integer :: plen
34
+
35
+    integer :: i, slen
36
+
37
+    slen = len(pattern)
38
+    plen = slen
39
+
40
+    ! Look for null terminator
41
+    do i = 1, slen
42
+      if (pattern(i:i) == char(0)) then
43
+        plen = i - 1
44
+        return
45
+      end if
46
+    end do
47
+
48
+    ! No null terminator found - return full length
49
+    ! (pattern was passed with explicit length, e.g., pattern(1:2))
50
+    ! Note: Don't use len_trim here as it would break whitespace patterns
51
+
52
+  end function pattern_len
53
+
2454
 end module ferp_kinds