// =========================================================================== // Copyright 2018 Autodesk, Inc. All rights reserved. // // Use of this software is subject to the terms of the Autodesk license // agreement provided at the time of installation or download, or which // otherwise accompanies this software in either electronic or hard copy form. // =========================================================================== // // Create Date: 15 Dec 2009 // // // // // // // string formValidObjectName(string $name) // // // Form the valid object name. It will replace all the // invalid characters with "_". If the string is not // start with English character, it will add an "a" at the // the beginning of the string. Non-ascii characters will // be treated as invalid. // If the passed in string is empty or it can't form the valid // object name string, it will return "defaultObject" instead. // // // string $name The original name string for generating the // valid object name. // // // string : The valid object name. // // // // // formValidObjectName("FBX Export"); // It will return "FBX_Export" // // formValidObjectName("2FBX Export"); // It will return "a2FBX_Export" // // formValidObjectName("_FBX Export"); // It will return "a_FBX_Export" // // formValidObjectName("FBX2 Export"); // It will return "FBX2_Export" // // formValidObjectName("F_BX Ex_port"); // It will return "F_BX_Ex_port" // // formValidObjectName("F!BX Ex#$port"); // It will return "F_BX_Ex__port" // // formValidObjectName(""); // It will return "defaultObject" // // formValidObjectName("_"); // It will return "a_" // // formValidObjectName("!"); // It will return "a_" // // formValidObjectName("b"); // It will return "b" // // // global proc string formValidObjectName(string $name) { // Default object name string $defaultName = "defaultObject"; // Empty string if(`size($name)` == 0) { return $defaultName; } // Replace all the invalid character with character "_" string $re = "[^a-zA-Z0-9_]"; string $result = `substitute $re $name "_"`; int $loopCount = 0; int $nameSize = `size($result)`; // Since substitute() will only find and replace // the first matched string, we need to call it // enough times to make sure all the invalid characters // have been replaced with "_". while(!isValidObjectName($result)) { // Multi bytes will be treated as invalid // and replaced with "_" $result = `substitute $re $result "_"`; // If we have passed all the characters if(++$loopCount > $nameSize) { break; } } if($loopCount > $nameSize) { // It might be because the first character // is "_" or a number // $result = "a" + $result; if(isValidObjectName($result)) { return $result; } return $defaultName; } return $result; }