Code covered by the BSD License  

Highlights from
isValidSymbol

image thumbnail

isValidSymbol

by

 

Check for valid variable, field or function name - fast C-Mex

isValidSymbol(S)
function L = isValidSymbol(S)     %#ok<INUSD,STOUT>
% ISVALIDSYMBOL - Check string to be a valid name [MEX]
% L = isValidSymbol(S)
% INPUT:
%   S: String or cell string.
%      Multi-dimensional CHAR arrays are rejected.
% OUTPUT:
%   L: LOGICAL scalar, if S is a string. LOGICAL array of the same size
%      as S, if S is a cell string.
%      L contains TRUE for valid symbols:
%      The 1st character must be a letter, the following must be a letter,
%      number or the underscore. The length must not exceed 63 characters.
%
% EXAMPLES:
%   isValidSymbol('a')          %  >> true
%   isValidSymbol('a1')         %  >> true
%   isValidSymbol('a_')         %  >> true
%   isValidSymbol({'_a', 'b'})  %  >> [false, true]
%   isValidSymbol('1a')         %  >> false
%   s = repmat('a', 1, 63);
%   isValidSymbol(s)            %  >> true
%   isValidSymbol([s, 'a'])     %  >> false
%
% NOTE: This function is equivalent to Matlab's ISVARNAME, but operates on cell
%   strings in addition, what is much faster than a loop over the cell elements.
%   While ISVARNAME is some percent faster for single strings, checking a {1 x
%   40} cell string is 16 times faster by isValidSymbol than by calling
%   ISVARNAME in a loop.
%   You can accelerate Matlab's GENVARNAME remarkably by using this function
%   instead of calling ISVARNAME is a loop.
%
% AIM OF THIS PROGRAM:
%   Actually the subfunction CheckCString was developed to support the save
%   creation of structs and MAT files in Mex functions. You can easily cut and
%   paste this function to your Mex-projects.
%
%   For checking Matlab strings, the conversion to C-strings wastes time.
%   Therefore the subfunction CheckMatlabString operates on the mxChar vectors
%   directly.
%
% COMPILATION: see isValidSymbol.c
%
% TEST: Run uTest_isValidSymbol to check validity and speed..
%
% Tested: Matlab 6.5, 7.7, 7.8, WinXP, 32bit
%         Compiler: LCC2.4/3.8, BCC5.5, OWC1.8, MSVC2008
% Assumed Compatibility: higher Matlab versions, Mac, Linux, 64bit.
% Author: Jan Simon, Heidelberg, (C) 2006-2010 matlab.THISYEAR(a)nMINUSsimon.de
%
% See also ValidSymbol, NAMELENGTHMAX, ISKEYWORD, ISVARNAME, GENVARNAME.

% $JRev: R0g V:034 Sum:/Tdrgmj3xs7S Date:04-Oct-2010 13:01:50 $
% $License: BSD $
% $UnitTest: uTest_isValidSymbol $
% $File: Tools\GLString\isValidSymbol.m $
% History:
% 025: 31-Dec-2007 15:41, 63 characters allowed.
% 026: 05-Jan-2008 14:33, TRUE/FALSE: Either Matlab6.5 or MEX.
% 032: 19-Aug-2010 19:10, Mex finished for the FEX.

% Initialize: ==================================================================
% Do the work: =================================================================
% This is a slower, but working M-version - prefer the C-Mex!
error(['JSimon:', mfilename, ':NoMex'], 'Cannot find compiled Mex file!');

% This is a slow M-implementation - use the MEX!
% For experimental reasons two different methods are implemented for cell string
% and strings. ISVARNAME is more efficient than this implementation!

% % Decide if only one name has to be tested or a list of names:
% if isa(Name, 'cell')
%    off = logical(0);
%
%    % Default answer:
%    isValid = true(size(Name));
%
%    % Loop over all input names:
%    for iName = 1:length(Name)
%       aName  = Name{iName};
%       letInd = find(isletter(aName) & aName < 123);
%       if or(isempty(letInd), length(aName) > 63)
%          % aName is empty, contain spaces or at least not one letter or
%          % the name is too long for Matlab5.3:
%          isValid(iName) = off;
%       elseif letInd(1) ~= 1
%          % First character is no letter:
%          isValid(iName) = off;
%       else  % Look for bad characters in the rest:
%          aName(letInd)             = [];  % 'a' to 'z' and 'A' to 'Z'
%          aName(findstr(aName, 95)) = [];  % Underscore
%          if or(any(aName < 48), any(aName > 57))
%             isValid(iName) = off;
%          end
%       end
%    end
%
% else  % Name is not a cell, assume it is a string.
%    if isempty(Name)
%       isValid = logical(0);
%    else
%       dName   = double(Name);
%       isValid = and(length(dName) <= 63, isletter(Name(1))) & ...
%          all((isletter(Name) & dName < 123) | dName == 95 | ...
%          (48 <= dName & dName <= 57));
%    end
% end
%
% return;

Contact us