Skip to content

Commit

Permalink
Treat PICS as case-insensitive in YAML. (project-chip#21432)
Browse files Browse the repository at this point in the history
  • Loading branch information
bzbarsky-apple authored and isiu-apple committed Sep 16, 2022
1 parent 263a55b commit ec616c5
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/app/tests/suites/pics/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ static_library("pics") {
"PICSBooleanExpressionParser.h",
"PICSBooleanReader.cpp",
"PICSBooleanReader.h",
"PICSNormalizer.cpp",
"PICSNormalizer.h",
]

cflags = [ "-Wconversion" ]
Expand Down
3 changes: 3 additions & 0 deletions src/app/tests/suites/pics/PICSBooleanExpressionParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

#include "PICSBooleanExpressionParser.h"
#include "PICSNormalizer.h"

#include <lib/support/CodeUtils.h>

Expand Down Expand Up @@ -150,6 +151,8 @@ bool PICSBooleanExpressionParser::EvaluateSubExpression(std::vector<std::string>
return !expr;
}

token = PICSNormalizer::Normalize(token);

index++;

if (PICS.find(token) == PICS.end())
Expand Down
2 changes: 2 additions & 0 deletions src/app/tests/suites/pics/PICSBooleanReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

#include "PICSBooleanReader.h"
#include "PICSNormalizer.h"

#include <lib/support/CodeUtils.h>

Expand Down Expand Up @@ -43,6 +44,7 @@ std::map<std::string, bool> PICSBooleanReader::Read(std::string filepath)

std::getline(ss, key, '=');
VerifyOrDieWithMsg(!key.empty(), chipTool, "Missing PICS key at line %u", lineNumber + 1);
key = PICSNormalizer::Normalize(key);

std::getline(ss, value);
if (value == "0")
Expand Down
30 changes: 30 additions & 0 deletions src/app/tests/suites/pics/PICSNormalizer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Copyright (c) 2022 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "PICSNormalizer.h"

#include <algorithm>
#include <cctype>

std::string PICSNormalizer::Normalize(std::string code)
{
// Convert to all-lowercase so people who mess up cases don't have things
// break on them in subtle ways.
std::transform(code.begin(), code.end(), code.begin(), [](unsigned char c) { return std::tolower(c); });

// TODO strip off "(Additional Context)" bits from the end of the code.
return code;
}
28 changes: 28 additions & 0 deletions src/app/tests/suites/pics/PICSNormalizer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright (c) 2022 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* @file
* @brief Declaration of a method that normalizes a PICS code.
*/

#include <string>

class PICSNormalizer
{
public:
static std::string Normalize(std::string code);
};

0 comments on commit ec616c5

Please sign in to comment.