0.2.0-dev Download ZIP
101 lines ·2.1 KB Raw
| 1 | // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics) |
| 2 | // SPDX-FileCopyrightText: 2021 Jorrit Rouwe |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | |
| 5 | #include <Jolt/Jolt.h> |
| 6 | |
| 7 | #include <Jolt/Core/StringTools.h> |
| 8 | |
| 9 | JPH_SUPPRESS_WARNINGS_STD_BEGIN |
| 10 | #include <cstdarg> |
| 11 | JPH_SUPPRESS_WARNINGS_STD_END |
| 12 | |
| 13 | JPH_NAMESPACE_BEGIN |
| 14 | |
| 15 | String StringFormat(const char *inFMT, ...) |
| 16 | { |
| 17 | char buffer[1024]; |
| 18 | |
| 19 | // Format the string |
| 20 | va_list list; |
| 21 | va_start(list, inFMT); |
| 22 | vsnprintf(buffer, sizeof(buffer), inFMT, list); |
| 23 | va_end(list); |
| 24 | |
| 25 | return String(buffer); |
| 26 | } |
| 27 | |
| 28 | void StringReplace(String &ioString, const string_view &inSearch, const string_view &inReplace) |
| 29 | { |
| 30 | size_t index = 0; |
| 31 | for (;;) |
| 32 | { |
| 33 | index = ioString.find(inSearch, index); |
| 34 | if (index == String::npos) |
| 35 | break; |
| 36 | |
| 37 | ioString.replace(index, inSearch.size(), inReplace); |
| 38 | |
| 39 | index += inReplace.size(); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | void StringToVector(const string_view &inString, Array<String> &outVector, const string_view &inDelimiter, bool inClearVector) |
| 44 | { |
| 45 | JPH_ASSERT(inDelimiter.size() > 0); |
| 46 | |
| 47 | // Ensure vector empty |
| 48 | if (inClearVector) |
| 49 | outVector.clear(); |
| 50 | |
| 51 | // No string? no elements |
| 52 | if (inString.empty()) |
| 53 | return; |
| 54 | |
| 55 | // Start with initial string |
| 56 | String s(inString); |
| 57 | |
| 58 | // Add to vector while we have a delimiter |
| 59 | size_t i; |
| 60 | while (!s.empty() && (i = s.find(inDelimiter)) != String::npos) |
| 61 | { |
| 62 | outVector.push_back(s.substr(0, i)); |
| 63 | s.erase(0, i + inDelimiter.length()); |
| 64 | } |
| 65 | |
| 66 | // Add final element |
| 67 | outVector.push_back(s); |
| 68 | } |
| 69 | |
| 70 | void VectorToString(const Array<String> &inVector, String &outString, const string_view &inDelimiter) |
| 71 | { |
| 72 | // Ensure string empty |
| 73 | outString.clear(); |
| 74 | |
| 75 | for (const String &s : inVector) |
| 76 | { |
| 77 | // Add delimiter if not first element |
| 78 | if (!outString.empty()) |
| 79 | outString.append(inDelimiter); |
| 80 | |
| 81 | // Add element |
| 82 | outString.append(s); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | String ToLower(const string_view &inString) |
| 87 | { |
| 88 | String out; |
| 89 | out.reserve(inString.length()); |
| 90 | for (char c : inString) |
| 91 | out.push_back((char)tolower(c)); |
| 92 | return out; |
| 93 | } |
| 94 | |
| 95 | const char *NibbleToBinary(uint32 inNibble) |
| 96 | { |
| 97 | static const char *nibbles[] = { "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" }; |
| 98 | return nibbles[inNibble & 0xf]; |
| 99 | } |
| 100 | |
| 101 | JPH_NAMESPACE_END |