From: <car...@us...> - 2012-03-16 03:05:31
|
Revision: 9914 http://octave.svn.sourceforge.net/octave/?rev=9914&view=rev Author: carandraug Date: 2012-03-16 03:05:24 +0000 (Fri, 16 Mar 2012) Log Message: ----------- strings: use of print_usage, texinfo guidelines, aesthetical changes, fix indentation, prune unecessary return statements before endfunction Modified Paths: -------------- trunk/octave-forge/main/strings/inst/base64decode.m trunk/octave-forge/main/strings/inst/base64encode.m trunk/octave-forge/main/strings/inst/editdistance.m trunk/octave-forge/main/strings/inst/strjoin.m trunk/octave-forge/main/strings/inst/strsort.m Modified: trunk/octave-forge/main/strings/inst/base64decode.m =================================================================== --- trunk/octave-forge/main/strings/inst/base64decode.m 2012-03-16 02:59:01 UTC (rev 9913) +++ trunk/octave-forge/main/strings/inst/base64decode.m 2012-03-16 03:05:24 UTC (rev 9914) @@ -14,9 +14,9 @@ ## this program; if not, see <http://www.gnu.org/licenses/>. ## -*- texinfo -*- -## @deftypefn {Function File} @var{rval}= {} base64decode(@var{code}) -## @deftypefnx {Function File} @var{rval}= {} base64decode(@var{code},@var{as_string}) -## convert a base64 @var{code} (a string of printable characters according to RFC 2045) +## @deftypefn {Function File} {@var{rval} =} base64decode (@var{code}) +## @deftypefnx {Function File} {@var{rval} =} base64decode (@var{code}, @var{as_string}) +## Convert a base64 @var{code} (a string of printable characters according to RFC 2045) ## into the original ASCII data set of range 0-255. If option @var{as_string} is ## passed, the return value is converted into a string. ## @@ -27,21 +27,15 @@ ## ##returns 'Hakuna Matata' ## @end group ## @end example -## @end deftypefn -## @seealso {base64encode} ## - -## -## Y = base64decode(X) -## -## Convert X into string of printable characters according to RFC 2045. -## The input may be a string or a matrix of integers in the range 0..255. -## ## See: http://www.ietf.org/rfc/rfc2045.txt ## -function z = base64decode(X,as_string) +## @seealso {base64encode} +## @end deftypefn + +function z = base64decode (X, as_string) if (nargin < 1 ) - usage("Y = base64decode(X); X need to be a 4-row N-col matrix"); + print_usage; elseif nargin == 1 as_string=false; endif @@ -50,32 +44,28 @@ error("base64decode is expecting integers in the range 0 .. 255"); endif - ## ## decompose strings into the 4xN matrices ## formatting issues. - ## if( rows(X) == 1 ) - Y=[]; - L=length(X); - for z=4:4:L - Y=[Y X(z-3:z)']; #keep adding columns - end - if min(size(Y))==1 - Y=reshape(Y,[L, 1]); - else - Y=reshape(Y,[4,L/4]); - end - X=Y; - Y=[]; + Y=[]; + L=length(X); + for z=4:4:L + Y=[Y X(z-3:z)']; #keep adding columns + end + if min(size(Y))==1 + Y=reshape(Y,[L, 1]); + else + Y=reshape(Y,[4,L/4]); + end + X=Y; + Y=[]; end X = toascii(X); Xa= X; - ## ## Work backwards. Starting at step in table, ## lookup the index of the element in the table. - ## ## 6-bit encoding table, plus 1 for padding ## 26*2 + 10 + 2 + 1 = 64 + 1, '=' is EOF stop mark. @@ -96,18 +86,18 @@ iaz = (Xa >= 'a').*(Xa <= 'z') > 0; Va(iaz)=Xa(iaz)-'a'+26; - i09 = (Xa >= '0').*(Xa <= '9') > 0; + i09 = (Xa >= '0').*(Xa <= '9') > 0; Va(i09)=Xa(i09)-'0'+52; is = (Xa == '/') ; Va(is) = 63; ip = (Xa == '+') ; Va(ip) = 62; ieq = (Xa == '=') ; Va(ieq) = 0; - clear is; clear ieq; clear ip; clear i09; + clear is; clear ieq; clear ip; clear i09; clear iaz; clear iAZ; clear Xa; clear X; Y=Va; clear Va; Y1=Y(1,:); - if (SRows > 1) + if (SRows > 1) Y2=Y(2,:); else Y2=zeros(1,SCols); @@ -119,7 +109,7 @@ Y3=zeros(1,SCols); end; - if (SRows > 3) + if (SRows > 3) Y4=Y(4,:); else Y4=zeros(1,SCols); @@ -147,10 +137,8 @@ z(3:3:end)=[]; end - ## ## FIXME ## is this expected behaviour? - ## if ( as_string ) L=length(z); while ( ( L > 0) && ( z(L)==0 ) ) @@ -159,14 +147,11 @@ z=char(z(1:L)); end - return - endfunction -%! + %!assert(base64decode(base64encode('Hakuna Matata'),true),'Hakuna Matata') %!assert(base64decode(base64encode([1:255])),[1:255]) %!assert(base64decode(base64encode('taken'),true),'taken') %!assert(base64decode(base64encode('sax'),true),'sax') %!assert(base64decode(base64encode('H'),true),'H') %!assert(base64decode(base64encode('Ta'),true),'Ta') -%! Modified: trunk/octave-forge/main/strings/inst/base64encode.m =================================================================== --- trunk/octave-forge/main/strings/inst/base64encode.m 2012-03-16 02:59:01 UTC (rev 9913) +++ trunk/octave-forge/main/strings/inst/base64encode.m 2012-03-16 03:05:24 UTC (rev 9914) @@ -2,8 +2,8 @@ ## This program is granted to the public domain. ## -*- texinfo -*- -## @deftypefn @var{Y} = {Function File} {} base64encode(@var{X}) -## @deftypefnx @var{Y} = {Function File} {} base64encode(@var{X}, @var{do_reshape}) +## @deftypefn {Function File} {@var{Y} =} base64encode (@var{X}) +## @deftypefnx {Function File} {@var{Y} =} base64encode (@var{X}, @var{do_reshape}) ## Convert X into string of printable characters according to RFC 2045. ## The input may be a string or a matrix of integers in the range 0..255. ## If want the output in the 1-row of strings format, pass the @@ -11,17 +11,19 @@ ## ## Example: ## @example +## @group ## base64encode('Hakuna Matata',true) ## ##returns 'SGFrdW5hIE1hdGF0YQ==' ## +## @end group ## @end example +## @seealso{base64decode} ## @end deftypefn -## @seealso{base64decode} -function Y = base64encode(X,do_reshape) +function Y = base64encode (X, do_reshape) if (nargin < 1) - usage("Y = base64encode(X,[do_reshape])"); + print_usage; elseif nargin != 2 do_reshape=false; endif @@ -71,6 +73,5 @@ Y = reshape(Y,[1, prod(size(Y))]); end endfunction -%! + %!assert(base64encode('Hakuna Matata',true),'SGFrdW5hIE1hdGF0YQ==') -%! Modified: trunk/octave-forge/main/strings/inst/editdistance.m =================================================================== --- trunk/octave-forge/main/strings/inst/editdistance.m 2012-03-16 02:59:01 UTC (rev 9913) +++ trunk/octave-forge/main/strings/inst/editdistance.m 2012-03-16 03:05:24 UTC (rev 9914) @@ -14,9 +14,10 @@ ## this program; if not, see <http://www.gnu.org/licenses/>. ## -*- texinfo -*- -## @deftypefn {Function File} [@var{dist},@var{L}] = {} editdistance(@var{string1},@var{string2},@var{weights}) -## computes the Levenshtein edit distance between the two strings. -## @var{string1} and @var{string2}. This operation is symmetrical. +## @deftypefn {Function File} {[@var{dist},@var{L}] =} editdistance (@var{string1}, @var{string2}, @var{weights}) +## Compute the Levenshtein edit distance between the strings @var{string1} and +## @var{string2}. This operation is symmetrical. +## ## The optional argument @var{weights} specifies weights for the ## deletion, matched, and insertion operations; by default it is set to ## +1, 0, +1 respectively, so that a least editdistance means a @@ -29,53 +30,50 @@ ## the other return value @var{L} is the distance matrix. ## ## @example -## @group +## @group ## editdistance('marry','marie') ## ##returns value +2 for the distance. ## @end group ## @end example ## ## @end deftypefn -## -function [dist,L]=editdistance(str1,str2,weights) - if(nargin < 2 || (nargin == 3 && length(weights) < 3) ) - print_usage(); - end - - L1=length(str1)+1; - L2=length(str2)+1; - L=zeros(L1,L2); - - if(nargin < 3) - g=+1;%insertion - m=+0;%match - d=+1;%deletion - else - g=weights(1); - m=weights(2); - d=weights(3); - end - +function [dist, L] = editdistance (str1, str2, weights) + if(nargin < 2 || (nargin == 3 && length(weights) < 3) ) + print_usage(); + end + + L1=length(str1)+1; + L2=length(str2)+1; + L=zeros(L1,L2); + + if(nargin < 3) + g=+1;%insertion + m=+0;%match + d=+1;%deletion + else + g=weights(1); + m=weights(2); + d=weights(3); + end - L(:,1)=[0:L1-1]'*g; - L(1,:)=[0:L2-1]*g; - - m4=0; - for idx=2:L1; - for idy=2:L2 - if(str1(idx-1)==str2(idy-1)) - score=m; - else - score=d; - end - m1=L(idx-1,idy-1) + score; - m2=L(idx-1,idy) + g; - m3=L(idx,idy-1) + g; - L(idx,idy)=min(m1,min(m2,m3)); - end + L(:,1)=[0:L1-1]'*g; + L(1,:)=[0:L2-1]*g; + + m4=0; + for idx=2:L1; + for idy=2:L2 + if(str1(idx-1)==str2(idy-1)) + score=m; + else + score=d; + end + m1=L(idx-1,idy-1) + score; + m2=L(idx-1,idy) + g; + m3=L(idx,idy-1) + g; + L(idx,idy)=min(m1,min(m2,m3)); end - - dist=L(L1,L2); - return -end + end + + dist=L(L1,L2); +endfunction Modified: trunk/octave-forge/main/strings/inst/strjoin.m =================================================================== --- trunk/octave-forge/main/strings/inst/strjoin.m 2012-03-16 02:59:01 UTC (rev 9913) +++ trunk/octave-forge/main/strings/inst/strjoin.m 2012-03-16 03:05:24 UTC (rev 9914) @@ -14,9 +14,9 @@ ## this program; if not, see <http://www.gnu.org/licenses/>. ## -*- texinfo -*- -## @deftypefn {Function File} @var{rval}= {} strjoin(@var{prefixstr},@var{stringcell}) -## @deftypefnx {Function File} @var{rval}= {} strjoin(@var{prefixstr},@var{varargs}) -## joins the strings in @var{stringcell} with the @var{prefixstr} like the list-join +## @deftypefn {Function File} {@var{rval} =} strjoin (@var{prefixstr}, @var{stringcell}) +## @deftypefnx {Function File} {@var{rval} =} strjoin (@var{prefixstr}, @var{varargs}) +## Joins the strings in @var{stringcell} with the @var{prefixstr} like the list-join ## function in Python; the second version allows usage with variable number of arguments. ## Note that, if using cell-array as a second argument, only 2 arguments are accepted. ## Also note that, both the arguments are strings or containers of strings (cells). @@ -30,17 +30,16 @@ ## ##returns 'Octave*Scilab*Lush*Yorick' ## @end group ## @end example -## @end deftypefn ## @seealso {strcmp} -## +## @end deftypefn -function rval=strjoin(spacer,varargin) +function rval = strjoin (spacer, varargin) if (nargin < 2) || (nargin > 2 && iscell(varargin{1}) ) print_usage(); end if iscell(varargin{1}) - varargin=varargin{1}; + varargin=varargin{1}; end rval=""; @@ -49,9 +48,7 @@ rval=strcat(rval,sprintf('%s%s',varargin{idx},spacer)); end rval=strcat(rval,varargin{L}); - return; -end -%! +endfunction + %!assert(strjoin("-","hello"),"hello") %!assert(strjoin('*',{'Octave','Scilab','Lush','Yorick'}),'Octave*Scilab*Lush*Yorick') -%! Modified: trunk/octave-forge/main/strings/inst/strsort.m =================================================================== --- trunk/octave-forge/main/strings/inst/strsort.m 2012-03-16 02:59:01 UTC (rev 9913) +++ trunk/octave-forge/main/strings/inst/strsort.m 2012-03-16 03:05:24 UTC (rev 9914) @@ -1,13 +1,17 @@ ## Author: Paul Kienzle <pki...@us...> ## This program is granted to the public domain. -# ... = strsort(...) -# Overloads the sort function to operate on strings. +## -*- texinfo -*- +## @deftypefn {Function File} {[@ldots{}] =} strsort (@ldots{}) +## Overloads the sort function to operate on strings. +## +## @seealso {sort} +## @end deftypefn # PKG_ADD dispatch ("sort", "strsort", "string") function [sorted,idx] = strsort(string,varargin) if nargout == 2 - [s,idx] = sort(toascii(string),varargin{:}); + [s,idx] = sort(toascii(string),varargin{:}); else s = sort(toascii(string),varargin{:}); endif This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |