Splits string str by separator sep and returns in pointer array carray. carray must be null when passed. Error is returned if already allocated. If limit is a positive number, splits into at most limit fields. If limit is negative or 0, same as omitting it. Trailing spaces of str are removed. If sep is empty string, splits by whitespace.
1511 implicit none
1512 character(*), intent(in):: str
1513 character(*), pointer:: carray(:)
1514 character(*), intent(in):: sep
1515 integer, intent(in), optional:: limit
1516 integer :: num, cur, i, limitnum
1517 character(STRING) :: substr
1518 logical :: end_flag
1519 continue
1520 if (present(limit)) then
1521 if (limit > 0) then
1522 limitnum = limit
1523 else
1524 limitnum = 0
1525 end if
1526 else
1527 limitnum = 0
1528 end if
1529 if (len(trim(sep)) == 0) then
1530 num = 1
1531 substr = str
1532
1533
1534 do
1535 cur = index(trim(substr), ' ')
1536 if (cur == 0) exit
1537 num = num + 1
1538 substr = adjustl(substr(cur + len(sep) :len(substr)))
1539 end do
1540 if (limitnum /= 0 .and. num > limitnum) num = limitnum
1541 allocate(carray(num))
1542 substr = str
1543 end_flag = .false.
1544 do i = 1, num
1545 cur = index(trim(substr), ' ')
1546 if (cur == 0 .or. i == num) end_flag = .true.
1547 if (end_flag) then
1548 carray(i) = substr
1549 exit
1550 else
1551 carray(i) = substr(1:cur - 1)
1552 end if
1553 substr = adjustl(substr(cur + len(sep) :len(substr)))
1554 end do
1555 else
1556 num = 1
1557 substr = str
1558
1559
1560 do
1561 cur = index(substr, trim(sep))
1562 if (cur == 0) exit
1563 num = num + 1
1564 substr = substr(cur + len(sep) :len(substr))
1565 end do
1566 if (limitnum /= 0 .and. num > limitnum) num = limitnum
1567 allocate(carray(num))
1568 substr = str
1569 end_flag = .false.
1570 do i = 1, num
1571 cur = index(substr, trim(sep))
1572 if (cur == 0 .or. i == num) end_flag = .true.
1573 if (end_flag) then
1574 carray(i) = substr
1575 exit
1576 else
1577 carray(i) = substr(1:cur - 1)
1578 end if
1579 substr = substr(cur + len(sep) :len(substr))
1580 end do
1581 end if
1582 return
Provides kind type parameter values.
integer, parameter, public string
Character length for string