*** strread.m	Tue Mar  8 21:30:15 2011
--- c:/programs/octave/3.3.91/share/octave/3.3.91/m/io/strread.m	Tue Mar  8 21:16:16 2011
***************
*** 90,96 ****
  ## @end itemize
  ##
  ## @item "delimiter"
! ## Any character in @var{value} will be used to split @var{str} into words.
  ##
  ## @item "emptyvalue"
  ## Parts of the output where no word is available is filled with @var{value}.
--- 90,102 ----
  ## @end itemize
  ##
  ## @item "delimiter"
! ## Any character in @var{value} will be used to split @var{str} into words 
! ## (default value = \"\n\").
! ##
! ## @item "whitespace"
! ## Any character in @var{value} will be interpreted as whitespace and
! ## trimmed; the string defining whitespace must be enclosed in double
! ## quotes for proper processing of special characters like \t.
  ##
  ## @item "emptyvalue"
  ## Parts of the output where no word is available is filled with @var{value}.
*************** function varargout = strread (str, forma
*** 146,152 ****
      endswitch
    endfor
    if (isempty (delimiter_str))
!     delimiter_str = white_spaces;
    endif
  
    ## Parse format string
--- 152,162 ----
      endswitch
    endfor
    if (isempty (delimiter_str))
!     if (~isempty (white_spaces))
!       delimiter_str = white_spaces;
! 	else
! 	  delimiter_str = "\n";
! 	endif
    endif
  
    ## Parse format string
*************** function varargout = strread (str, forma
*** 157,163 ****
    nfields = length (idx) - length (idx_star);
  
    if (max (nargout, 1) != nfields)
!     error ("strread: the number of output variables must match that specified byFORMAT");
    endif
  
    ## Remove comments
--- 167,173 ----
    nfields = length (idx) - length (idx_star);
  
    if (max (nargout, 1) != nfields)
!     error ("strread: the number of output variables must match that specified by FORMAT");
    endif
  
    ## Remove comments
*************** function varargout = strread (str, forma
*** 188,194 ****
  
    ## Determine the number of words per line
    format = strrep (format, "%", " %");
!   [~, ~, ~, fmt_words] = regexp (format, '[^ ]+');
  
    num_words_per_line = numel (fmt_words);
    for m = 1:numel(fmt_words)
--- 198,204 ----
  
    ## Determine the number of words per line
    format = strrep (format, "%", " %");
!   [~, ~, ~, fmt_words] = regexp (format, "[^ ]+");
  
    num_words_per_line = numel (fmt_words);
    for m = 1:numel(fmt_words)
*************** function varargout = strread (str, forma
*** 202,209 ****
--- 212,247 ----
      endif
    endfor
  
+   if (~isempty (white_spaces))
+     ## Check for overlapping whitespaces and delimiters & trim whitespace
+     [ovlp, iw, ~] = intersect (white_spaces, delimiter_str);
+     if (~isempty (ovlp))
+       ## Remove delimiter chars from white_spaces
+ 	  white_spaces = cell2mat (strsplit (white_spaces, white_spaces(iw)));
+     endif
+   endif
+ 
+   if (~isempty (white_spaces))
+     ## Remove repeated white_space chars. First find white_spacea positions
+     idx = strchr (str, white_spaces);
+     ## Find repeated white_spaces
+     idx2 = ~(idx(2:end) - idx(1:end-1) - 1);
+     ## Set al whitespace chars to spaces
+     ## FIXME: this implies real spaces are always part of white_spaces
+     str(idx(find (idx))) = ' ';
+     ## Set all repeated white_space to \0
+     str(idx(find (idx2))) = "\0";
+     str = strsplit (str, "\0");
+     ## Reconstruct trimmed str
+     str = cell2mat (str);
+   endif
+ 
    ## Split 'str' into words
    words = split_by (str, delimiter_str);
+   if (~isempty (white_spaces))
+     ## Trim leading and trailing white_spaces
+     words = strtrim (words);
+   endif
    num_words = numel (words);
    num_lines = ceil (num_words / num_words_per_line);
  
*************** function varargout = strread (str, forma
*** 239,249 ****
  endfunction
  
  function out = split_by (text, sep)
!   sep = union (sep, "\n");
!   pat = sprintf ('[^%s]+', sep);
    [~, ~, ~, out] = regexp (text, pat);
    out(cellfun (@isempty, out)) = {""};
-   out = strtrim (out);
  endfunction
  
  %!test
--- 277,286 ----
  endfunction
  
  function out = split_by (text, sep)
!   sep = union (sep, "\n");  # Why would newline always have to be a separator?
!   pat = sprintf ("[^%s]+", sep);
    [~, ~, ~, out] = regexp (text, pat);
    out(cellfun (@isempty, out)) = {""};
  endfunction
  
  %!test
