Skip to content

Commit f87f95a

Browse files
committed
Rename function and move throw statement to end of prepare_script
1 parent 69ca2be commit f87f95a

2 files changed

Lines changed: 18 additions & 17 deletions

File tree

include/ur_client_library/primary/primary_client.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ class PrimaryClient
351351

352352
ScriptInfo prepare_script(std::string script, std::string script_name);
353353
std::vector<std::string> strip_comments_and_whitespace(std::vector<std::string> script_lines);
354-
std::string truncate_and_check_script_name(std::string candidate_name);
354+
std::string truncate_script_name(std::string candidate_name);
355355

356356
PrimaryParser parser_;
357357
std::shared_ptr<PrimaryConsumer> consumer_;

src/primary/primary_client.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ std::vector<std::string> PrimaryClient::strip_comments_and_whitespace(std::vecto
347347
return stripped_script;
348348
}
349349

350-
std::string PrimaryClient::truncate_and_check_script_name(const std::string candidate_name)
350+
std::string PrimaryClient::truncate_script_name(const std::string candidate_name)
351351
{
352352
std::string final_name = candidate_name;
353353
// Limit script name length to 31, to ensure backwards compatibility
@@ -356,14 +356,7 @@ std::string PrimaryClient::truncate_and_check_script_name(const std::string cand
356356
final_name = final_name.substr(0, 31);
357357
URCL_LOG_WARN("Given script name was too long, and has been truncated. New script name is: %s", final_name.c_str());
358358
}
359-
// Validate script_name
360-
static const std::regex valid_name(R"(^[A-Za-z_][A-Za-z0-9_]*$)");
361-
if (!std::regex_match(final_name, valid_name))
362-
{
363-
throw urcl::ScriptCodeSyntaxException("Invalid script name: '" + final_name +
364-
"'. Can only contain letters, numbers and underscores. First character "
365-
"must be a letter or underscore.");
366-
}
359+
367360
return final_name;
368361
}
369362

@@ -387,14 +380,13 @@ ScriptInfo PrimaryClient::prepare_script(std::string script, std::string script_
387380
// Assign name according to inputs
388381
std::string actual_script_name = script_name.empty() ? "script_" + std::to_string(current_time) : script_name;
389382

390-
// Check that the final name is valid
391-
actual_script_name = truncate_and_check_script_name(actual_script_name);
392-
393383
ScriptTypes actual_script_type = urcl::primary_interface::ScriptTypes::DEF;
394384
// Is the script wrapped in a function definition? If not add one
395385
if (stripped_script[0].substr(0, 4).find("def ") == script.npos &&
396386
stripped_script[0].substr(0, 4).find("sec ") == script.npos)
397387
{
388+
// Check that the final name is not too long
389+
actual_script_name = truncate_script_name(actual_script_name);
398390
std::string definition = "def " + actual_script_name + "():";
399391
std::string end = "end";
400392
// Add indentation to the existing script code
@@ -411,23 +403,32 @@ ScriptInfo PrimaryClient::prepare_script(std::string script, std::string script_
411403
{
412404
size_t name_end = stripped_script[0].find("(");
413405
std::string name_in_script = stripped_script[0].substr(4, name_end - 4);
414-
if (stripped_script[0].find("def") != stripped_script[0].npos)
406+
if (stripped_script[0].substr(0, 4).find("def ") != stripped_script[0].npos)
415407
{
416408
actual_script_type = ScriptTypes::DEF;
417409
}
418410
else
419411
{
420412
actual_script_type = ScriptTypes::SEC;
421413
}
422-
// Check that the script name is valid, replace it if it is not
423-
actual_script_name = truncate_and_check_script_name(name_in_script);
414+
// Check that the script name is not too long, replace it, if it is
415+
actual_script_name = truncate_script_name(name_in_script);
424416
if (actual_script_name.size() != name_in_script.size())
425417
{
426418
stripped_script[0].replace(stripped_script[0].find(name_in_script), name_in_script.size(), actual_script_name);
427419
}
428420
}
429421

430-
if (stripped_script.back().find("end") == script.npos)
422+
// Validate script_name
423+
static const std::regex valid_name(R"(^[A-Za-z_][A-Za-z0-9_]*$)");
424+
if (!std::regex_match(actual_script_name, valid_name))
425+
{
426+
throw urcl::ScriptCodeSyntaxException("Invalid script name: '" + actual_script_name +
427+
"'. Can only contain letters, numbers and underscores. First character "
428+
"must be a letter or underscore.");
429+
}
430+
431+
if (stripped_script.back().substr(0, 3).find("end") == script.npos)
431432
{
432433
throw urcl::ScriptCodeSyntaxException("Script contains either function definition or secondary process "
433434
"definition, "

0 commit comments

Comments
 (0)