Skip to content

Commit

Permalink
support backslash directory separator on WIN32
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbartholomew committed Feb 29, 2024
1 parent 01d1a0e commit 809c206
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 12 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ LIB_SRC = \
core/libjsonnet.cpp \
core/parser.cpp \
core/pass.cpp \
core/path_utils.cpp \
core/static_analysis.cpp \
core/string_utils.cpp \
core/vm.cpp \
Expand Down
2 changes: 2 additions & 0 deletions core/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ cc_library(
"libjsonnet.cpp",
"parser.cpp",
"pass.cpp",
"path_utils.cpp",
"static_analysis.cpp",
"string_utils.cpp",
"vm.cpp",
Expand All @@ -21,6 +22,7 @@ cc_library(
"lexer.h",
"parser.h",
"pass.h",
"path_utils.h",
"state.h",
"static_analysis.h",
"static_error.h",
Expand Down
2 changes: 2 additions & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ set(LIBJSONNET_HEADERS
../include/libjsonnet_fmt.h
parser.h
pass.h
path_utils.h
state.h
static_analysis.h
static_error.h
Expand All @@ -24,6 +25,7 @@ set(LIBJSONNET_SOURCE
libjsonnet.cpp
parser.cpp
pass.cpp
path_utils.cpp
static_analysis.cpp
string_utils.cpp
vm.cpp)
Expand Down
39 changes: 39 additions & 0 deletions core/path_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright 2024 Google Inc. All rights reserved.
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 "path_utils.h"
#include <string>

namespace jsonnet::internal {

// C++17 adds <filesystem>
// We could consider using that instead.

#if _WIN32
static const char DIR_SEPARATORS[] = "\\/";
#else
static const char DIR_SEPARATORS[] = "/";
#endif

std::string path_dir_with_trailing_separator(const std::string &path) {
size_t last_slash = path.find_last_of(DIR_SEPARATORS);
if (last_slash != std::string::npos) {
return path.substr(0, last_slash + 1);
}
return "";
}

} // namespace jsonnet::internal
34 changes: 34 additions & 0 deletions core/path_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Copyright 2024 Google Inc. All rights reserved.
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.
*/

#ifndef JSONNET_PATH_UTILS_H
#define JSONNET_PATH_UTILS_H

#include <string>

namespace jsonnet::internal {

/** Get everything except the filename from a path. If the path is just a filename, returns "".
*
* The result includes the trailing directory separator (if there is one in the input).
* Exact behaviour is platform-specific (different platforms use different directory separators).
*/
std::string path_dir_with_trailing_separator(const std::string &path);

} // namespace jsonnet::internal

#endif

18 changes: 6 additions & 12 deletions core/vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ limitations under the License.
#include "static_analysis.h"
#include "string_utils.h"
#include "vm.h"
#include "path_utils.h"

namespace jsonnet::internal {

Expand All @@ -50,17 +51,6 @@ using json = nlohmann::json;

namespace {

/** Turn a path e.g. "/a/b/c" into a dir, e.g. "/a/b/". If there is no path returns "".
*/
std::string dir_name(const std::string &path)
{
size_t last_slash = path.rfind('/');
if (last_slash != std::string::npos) {
return path.substr(0, last_slash + 1);
}
return "";
}

/** Stack frames.
*
* Of these, FRAME_CALL is the most special, as it is the only frame the stack
Expand Down Expand Up @@ -796,7 +786,11 @@ class Interpreter {
*/
ImportCacheValue *importData(const LocationRange &loc, const LiteralString *file)
{
std::string dir = dir_name(loc.file);
// `dir` is passed to the importCallback, which may be externally defined.
// For backwards compatibility, we need to keep the trailing directory separator.
// For example, the default callback in libjsonnet.cpp joins paths with simple
// string concatenation. Other (external) implementations might do the same.
std::string dir = path_dir_with_trailing_separator(loc.file);

const UString &path = file->value;

Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"core/lexer.cpp",
"core/parser.cpp",
"core/pass.cpp",
"core/path_utils.cpp",
"core/static_analysis.cpp",
"core/string_utils.cpp",
"core/vm.cpp",
Expand Down

0 comments on commit 809c206

Please sign in to comment.