KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: A utility to validate 4D method and object names
PRODUCT: 4D | VERSION: 15.1 | PLATFORM: Mac & Win
Published On: April 13, 2016

4D established a set of rules on how various objects in the 4D language are to be named. This utility will vlidate all 4D Language object names except for Sets, Named Selections and Processes, all of which can have a name exceeding 31 characters in length.

If (True)
    If (False)
       Begin SQL
       /*
       Name: UTIL_Is4DIdentifier
       Path: UTIL_Is4DIdentifier

       Purpose: Check to see if the passed value is valid as a 4D identifier.
       4D identifiers follow these rules:
       -A name must begin with an alphabetic character or underscore.
       -Thereafter, the name can include alphabetic characters, numeric characters,
       the space character, and the underscore character.
       -Periods, slashes, quotation marks and colons are not allowed.
       -Characters reserved for use as operators, such as * and +, are not allowed.
       -4D ignores any trailing spaces.
       -See https://doc.4d.com/4Dv15/help/Title/en/page701.html

       $1 - TEXT - 4D method or object name
       */
       End SQL
    End if
    C_TEXT($MethodName_T)
    $MethodName_T:=Current method name
    //===================== Declare Variables ==================================
    //method_parameters_declarations
    C_TEXT($1;$Identifier_T)
    C_BOOLEAN($0;$IsIdentifier_B)
    //--------------------------------------------------------------------------
    //method_wide_constants_declarations
    //--------------------------------------------------------------------------
    //local_variable_declarations
    C_LONGINT($Len;$Params_L)
    C_TEXT($Pattern_T)

End if
//====================== Initialize and Setup ================================

$Params_L:=Count parameters
$Identifier_T:=$1

//======================== Method Actions ==================================

// Note: \w is Regex shorthand for alphanumeric or underscore, e.g. [a-zA-Z0-9_]
//
$Pattern_T:="\\w[\\w ]+\\w"
$IsIdentifier_B:=Match regex($Pattern_T;$Identifier_T;1)

If ($IsIdentifier_B)
    // Identifier length limited to 31 characters, not counting <> on interprocess vars and arrays
    //
    $Len:=31
    If (Position("<>";$Identifier_T)=1)
    $Len:=33
    End if

    If (Length($Identifier_T)>$Len)
    $IsIdentifier_B:=False
    End if
End if

//======================== Clean up and Exit =================================

$0:=$IsIdentifier_B