Skip to content

Commit

Permalink
Squashed 'externals/nitro/' changes from 72cd86cbf..49f6338d2
Browse files Browse the repository at this point in the history
49f6338d2 latest from CODA-OSS (#581)

git-subtree-dir: externals/nitro
git-subtree-split: 49f6338d2998fc6b4d033bf1456f1fb1bcb53567
  • Loading branch information
Dan Smith committed Sep 12, 2023
1 parent 369de34 commit a5674e7
Show file tree
Hide file tree
Showing 25 changed files with 368 additions and 227 deletions.
29 changes: 26 additions & 3 deletions externals/coda-oss/.github/workflows/build_unittest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ jobs:
# searchFolder: D:\a\nitro\nitro\${{ matrix.platform }}\${{ matrix.configuration }}
# runInParallel: true

build-linux-cmake:
build-linux-cmake-default:
strategy:
matrix:
os: [ubuntu-latest]
Expand All @@ -109,8 +109,7 @@ jobs:
run: |
env
which python
mkdir target
cd target
mkdir target && cd target
cmake .. -DCMAKE_INSTALL_PREFIX=install${{ matrix.os }}CMake-Github -DPYTHON_VERSION=${{ matrix.python-version }} -DENABLE_SWIG=ON
- name: build
run: |
Expand All @@ -127,6 +126,30 @@ jobs:
cd target
cmake --build . --target install
build-linux-cmake:
strategy:
matrix:
os: [ubuntu-latest]
configuration: [Debug, Release]
name: ${{ matrix.os }}-${{ matrix.configuration }}-CMake
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- name: configure
run: |
mkdir out && cd out
cmake .. -DENABLE_PYTHON=OFF
- name: build
run: |
cd out
# "-j" spawns too many processes causing GCC to crash
cmake --build . --config ${{ matrix.configuration }} -j 12
- name: test
# should run w/o install
run: |
cd out
ctest -C ${{ matrix.configuration }} --output-on-failure
build-waf:
strategy:
matrix:
Expand Down
10 changes: 7 additions & 3 deletions externals/coda-oss/modules/c++/include/TestCase.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
*
*/

#ifndef __TEST_CASE_H__
#define __TEST_CASE_H__
#pragma once
#ifndef CODA_OSS_TestCase_h_INCLUDED_
#define CODA_OSS_TestCase_h_INCLUDED_

#ifdef __cplusplus

Expand Down Expand Up @@ -75,6 +75,10 @@ inline std::string toString(const TX& X)
{
return str::toString(X);
}
inline std::string toString(const coda_oss::u8string& X)
{
return str::to_native(X);
}

template<typename TX>
inline void diePrintf_(const char* format, const std::string& testName, const char* file, const char* func, int line,
Expand Down Expand Up @@ -276,4 +280,4 @@ inline int main(TFunc f)

#endif

#endif
#endif // CODA_OSS_TestCase_h_INCLUDED_
2 changes: 1 addition & 1 deletion externals/coda-oss/modules/c++/include/UnitTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ inline void assert_almost_eq(const std::string& testName, long double X1, long d
#define TEST_ASSERT_EQ_MSG(msg, X1, X2) testName, Microsoft::VisualStudio::CppUnitTestFramework::Logger::WriteMessage(msg.c_str()); TEST_ASSERT_EQ(X1, X2)

#undef TEST_FAIL_MSG
#define TEST_FAIL_MSG(msg) { (void)testName; Microsoft::VisualStudio::CppUnitTestFramework::Assert::Fail(str::toWString(msg).c_str()); }
#define TEST_FAIL_MSG(msg) { (void)testName; Microsoft::VisualStudio::CppUnitTestFramework::Assert::Fail(str::details::to_wstring(msg).c_str()); }

#undef TEST_EXCEPTION
#undef TEST_THROWS
Expand Down
25 changes: 8 additions & 17 deletions externals/coda-oss/modules/c++/str/include/str/Convert.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,27 +85,25 @@ inline std::string toString(const std::string& value)
{
return value;
}
// can't be a template; `bool` overload above is a better match
// Prevent the template above from getting used; instead, use routines from **Encoding.h**.
std::string toString(const std::wstring&) = delete;
std::string toString(const std::u16string&) = delete;
std::string toString(const std::u32string&) = delete;
std::string toString(const coda_oss::u8string&) = delete;
std::string toString(const str::W1252string&) = delete;

inline std::string toString(std::string::const_pointer pStr)
{
return toString(std::string(pStr));
}

// The resultant `std::string`s have "native" encoding (which is lost) depending
// on the platform: UTF-8 on Linux and Windows-1252 on Windows.
CODA_OSS_API std::string toString(const coda_oss::u8string&);
CODA_OSS_API std::string toString(const str::W1252string&);
CODA_OSS_API std::string toString(const std::wstring&); // input is UTF-16 or UTF-32 depending on the platform
// can't be a template; `bool` overload above is a better match
std::string toString(std::wstring::const_pointer) = delete; // only used in unittests
std::string toString(std::u16string::const_pointer) = delete; // only used in unittests
std::string toString(std::u32string::const_pointer) = delete; // only used in unittests

CODA_OSS_API coda_oss::u8string u8FromString(const std::string&); // platform determines Windows-1252 or UTF-8 input

inline std::ostream& operator<<(std::ostream& os, const coda_oss::u8string& s)
{
os << toString(s);
os << to_native(s);
return os;
}

Expand All @@ -127,13 +125,6 @@ inline std::string toString(const T& real, const T& imag)
return toString(std::complex<T>(real, imag));
}

CODA_OSS_API std::wstring toWString(const std::string&); // platform determines Windows-1252 or UTF-8 input and output encoding
CODA_OSS_API std::wstring toWString(const coda_oss::u8string&); // platform determines UTF-16 or UTF-32 output encoding
CODA_OSS_API std::wstring toWString(const str::W1252string&); // platform determines UTF-16 or UTF-32 output encoding

CODA_OSS_API coda_oss::u8string u8FromWString(const std::wstring&); // platform determines UTF16 or UTF-32 input


template <typename T>
T toType(const std::string& s)
{
Expand Down
126 changes: 108 additions & 18 deletions externals/coda-oss/modules/c++/str/include/str/Encoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@

namespace str
{
namespace details
{
template <typename TReturn, typename TChar>
inline auto cast(const TChar* s)
{
Expand All @@ -52,24 +54,26 @@ inline auto cast(const TChar* s)
static_assert(sizeof(*retval) == sizeof(*s), "sizeof(*TReturn) != sizeof(*TChar)");
return retval;
}
}
template <typename TBasicStringT, typename TChar>
inline auto c_str(const std::basic_string<TChar>& s)
{
using return_t = typename TBasicStringT::const_pointer;
return cast<return_t>(s.c_str());
return details::cast<return_t>(s.c_str());
}
template <typename TBasicStringT, typename TChar>
inline TBasicStringT str(const std::basic_string<TChar>& s)
inline auto str(const std::basic_string<TChar>& s)
{
return TBasicStringT(c_str<TBasicStringT>(s), s.length()); // avoid extra strlen() call
}
template <typename TBasicStringT, typename TChar>
inline TBasicStringT make_string(TChar* p)
{
using return_t = typename TBasicStringT::const_pointer;
return cast<return_t>(p); // copy into RV
using const_pointer = typename TBasicStringT::const_pointer;
return details::cast<const_pointer>(p); // copy into RV
}

/************************************************************************/
// When the encoding is important, we want to "traffic" in coda_oss::u8string (UTF-8), not
// str::W1252string (Windows-1252) or std::string (unknown). Make it easy to get those from other encodings.
CODA_OSS_API coda_oss::u8string to_u8string(str::W1252string::const_pointer, size_t);
Expand All @@ -79,43 +83,129 @@ inline coda_oss::u8string to_u8string(coda_oss::u8string::const_pointer p, size_
{
return coda_oss::u8string(p, sz);
}
// Explicit overloads so template can be used for a different purpose.
inline auto to_u8string(const coda_oss::u8string& s)
{
return to_u8string(s.c_str(), s.length());
}
inline auto to_u8string(const str::W1252string& s)
{
return to_u8string(s.c_str(), s.length());
}
inline auto to_u8string(const std::u16string& s)
{
return to_u8string(s.c_str(), s.length());
}
inline auto to_u8string(const std::u32string& s)
{
return to_u8string(s.c_str(), s.length());
}
// These two routines are "dangerous" as they make it easy to convert
// a `char*` **already** in UTF-8 encoding to UTF-8; the result is garbage.
// Use u8FromNative() or u8FromNative() which is a bit more explicit.
coda_oss::u8string to_u8string(std::string::const_pointer, size_t) = delete;
coda_oss::u8string to_u8string(std::wstring::const_pointer, size_t) = delete;

// Template parameter specifies how `std::string` is encoded. As opposed
// to figuring it out a run-time based on the platform.
template <typename TBasicString>
inline auto to_u8string(const std::string& s) // UTF-8 or Windows-1252
{
return to_u8string(str::c_str<TBasicString>(s), s.length());
}
template <typename TBasicString>
inline auto to_u8string(const std::wstring& s) // UTF-16 or UTF-32
{
return to_u8string(str::c_str<TBasicString>(s), s.length());
}

/************************************************************************/
// UTF-16 is the default on Windows.
CODA_OSS_API std::u16string to_u16string(coda_oss::u8string::const_pointer, size_t);
CODA_OSS_API std::u16string to_u16string(str::W1252string::const_pointer, size_t);
inline auto to_u16string(const coda_oss::u8string& s)
{
return to_u16string(s.c_str(), s.length());
}
inline auto to_u16string(const str::W1252string& s)
{
return to_u16string(s.c_str(), s.length());
}

/************************************************************************/
// UTF-32 is convenient because each code-point is a single 32-bit integer.
// It's typically std::wstring::value_type on Linux, but NOT Windows.
CODA_OSS_API std::u32string to_u32string(coda_oss::u8string::const_pointer, size_t);
CODA_OSS_API std::u32string to_u32string(str::W1252string::const_pointer, size_t);
inline auto to_u32string(const coda_oss::u8string& s)
{
return to_u32string(s.c_str(), s.length());
}
inline auto to_u32string(const str::W1252string& s)
{
return to_u32string(s.c_str(), s.length());
}

/************************************************************************/
// Windows-1252 (almost the same as ISO8859-1) is the default single-byte encoding on Windows.
CODA_OSS_API str::W1252string to_w1252string(coda_oss::u8string::const_pointer p, size_t sz);
inline auto to_w1252string(const coda_oss::u8string& s)
{
return to_w1252string(s.c_str(), s.length());
}

// These two routines are "dangerous" as they make it easy to convert
// a `char*` **already** in UTF-8 encoding to UTF-8; the result is garbage.
// Use u8FromString() or u8FromWString() which is a bit more explicit.
coda_oss::u8string to_u8string(std::string::const_pointer, size_t) = delete;
coda_oss::u8string to_u8string(std::wstring::const_pointer, size_t) = delete;
/************************************************************************/

template<typename CharT>
inline auto to_u8string(const std::basic_string<CharT>& s)
inline auto u8FromNative(const std::string& s) // platform determines Windows-1252 or UTF-8 input
{
return to_u8string(s.c_str(), s.length());
#if _WIN32
const auto p = str::c_str<str::W1252string>(s); // std::string is Windows-1252 on Windows
#else
const auto p = str::c_str<coda_oss::u8string>(s); // assume std::string is UTF-8 on any non-Windows platform
#endif
return str::to_u8string(p, s.length());
}
template <typename CharT>
inline auto to_u16string(const std::basic_string<CharT>& s)

namespace details
{
return to_u16string(s.c_str(), s.length());
inline auto c_str(const std::wstring& s)
{
#if _WIN32
return str::c_str<std::u16string>(s); // std::wstring is UTF-16 on Windows
#else
return str::c_str<std::u32string>(s); // assume std::wstring is UTF-32 on any non-Windows platform
#endif
}
template <typename CharT>
inline auto to_u32string(const std::basic_string<CharT>& s)
}
inline auto u8FromNative(const std::wstring& s) // platform determines UTF16 or UTF-32 input
{
return to_u32string(s.c_str(), s.length());
return str::to_u8string(details::c_str(s), s.length());
}

/************************************************************************/

// The resultant `std::string`s have "native" encoding (which is lost) depending
// on the platform: UTF-8 on Linux and Windows-1252 on Windows.
namespace details
{
inline std::string to_string(const std::string& s)
{
return s;
}
CODA_OSS_API std::string to_string(const coda_oss::u8string&);
CODA_OSS_API std::string to_string(const std::wstring&); // input is UTF-16 or UTF-32 depending on the platform
CODA_OSS_API std::wstring to_wstring(const std::string&); // platform determines Windows-1252 or UTF-8 input and output encoding
CODA_OSS_API std::wstring to_wstring(const coda_oss::u8string&); // platform determines UTF-16 or UTF-32 output encoding
}
namespace testing
{
CODA_OSS_API std::string to_string(const str::W1252string&);
CODA_OSS_API std::wstring to_wstring(const str::W1252string&); // platform determines UTF-16 or UTF-32 output encoding
}

inline std::string to_native(const coda_oss::u8string& s) // cf., std::filesystem::native(), https://en.cppreference.com/w/cpp/filesystem/path/native
{
return details::to_string(s);
}

}
Expand Down
Loading

0 comments on commit a5674e7

Please sign in to comment.