1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-10-05 19:31:04 +00:00

Introduce std::string_view into wider use (#14368)

This commit is contained in:
sfan5 2024-02-17 15:35:33 +01:00 committed by GitHub
parent fa47af737f
commit 6ca214fefc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
74 changed files with 501 additions and 456 deletions

View file

@ -541,11 +541,9 @@ bool IClientMediaDownloader::checkAndLoad(
// Compute actual checksum of data
std::string data_sha1;
{
SHA1 data_sha1_calculator;
data_sha1_calculator.addBytes(data.c_str(), data.size());
unsigned char *data_tmpdigest = data_sha1_calculator.getDigest();
data_sha1.assign((char*) data_tmpdigest, 20);
free(data_tmpdigest);
SHA1 ctx;
ctx.addBytes(data);
data_sha1 = ctx.getDigest();
}
// Check that received file matches announced checksum

View file

@ -67,7 +67,7 @@ bool FileCache::loadByPath(const std::string &path, std::ostream &os)
return !bad;
}
bool FileCache::updateByPath(const std::string &path, const std::string &data)
bool FileCache::updateByPath(const std::string &path, std::string_view data)
{
createDir();
std::ofstream file(path.c_str(), std::ios_base::binary |
@ -80,13 +80,13 @@ bool FileCache::updateByPath(const std::string &path, const std::string &data)
return false;
}
file.write(data.c_str(), data.length());
file << data;
file.close();
return !file.fail();
}
bool FileCache::update(const std::string &name, const std::string &data)
bool FileCache::update(const std::string &name, std::string_view data)
{
std::string path = m_dir + DIR_DELIM + name;
return updateByPath(path, data);

View file

@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <iostream>
#include <string>
#include <string_view>
class FileCache
{
@ -31,7 +32,7 @@ public:
*/
FileCache(const std::string &dir) : m_dir(dir) {}
bool update(const std::string &name, const std::string &data);
bool update(const std::string &name, std::string_view data);
bool load(const std::string &name, std::ostream &os);
bool exists(const std::string &name);
@ -43,5 +44,5 @@ private:
void createDir();
bool loadByPath(const std::string &path, std::ostream &os);
bool updateByPath(const std::string &path, const std::string &data);
bool updateByPath(const std::string &path, std::string_view data);
};

View file

@ -804,7 +804,7 @@ ShaderInfo ShaderSource::generateShader(const std::string &name,
}
void dumpShaderProgram(std::ostream &output_stream,
const std::string &program_type, const std::string &program)
const std::string &program_type, std::string_view program)
{
output_stream << program_type << " shader program:" << std::endl <<
"----------------------------------" << std::endl;

View file

@ -192,4 +192,4 @@ public:
IWritableShaderSource *createShaderSource();
void dumpShaderProgram(std::ostream &output_stream,
const std::string &program_type, const std::string &program);
const std::string &program_type, std::string_view program);

View file

@ -404,7 +404,7 @@ private:
// Generate image based on a string like "stone.png" or "[crack:1:0".
// if baseimg is NULL, it is created. Otherwise stuff is made on it.
// source_image_names is important to determine when to flush the image from a cache (dynamic media)
bool generateImagePart(std::string part_of_name, video::IImage *& baseimg, std::set<std::string> &source_image_names);
bool generateImagePart(std::string_view part_of_name, video::IImage *& baseimg, std::set<std::string> &source_image_names);
/*! Generates an image from a full string like
* "stone.png^mineral_coal.png^[crack:1:0".
@ -412,7 +412,7 @@ private:
* The returned Image should be dropped.
* source_image_names is important to determine when to flush the image from a cache (dynamic media)
*/
video::IImage* generateImage(const std::string &name, std::set<std::string> &source_image_names);
video::IImage* generateImage(std::string_view name, std::set<std::string> &source_image_names);
// Thread-safe cache of what source images are known (true = known)
MutexedMap<std::string, bool> m_source_image_existence;
@ -593,7 +593,7 @@ static void draw_crack(video::IImage *crack, video::IImage *dst,
// Brighten image
void brighten(video::IImage *image);
// Parse a transform name
u32 parseImageTransform(const std::string& s);
u32 parseImageTransform(std::string_view s);
// Apply transform to image dimension
core::dimension2d<u32> imageTransformDimension(u32 transform, core::dimension2d<u32> dim);
// Apply transform to image data
@ -963,7 +963,8 @@ static video::IImage *createInventoryCubeImage(
return result;
}
video::IImage* TextureSource::generateImage(const std::string &name, std::set<std::string> &source_image_names)
video::IImage* TextureSource::generateImage(std::string_view name,
std::set<std::string> &source_image_names)
{
// Get the base image
@ -1024,15 +1025,15 @@ video::IImage* TextureSource::generateImage(const std::string &name, std::set<st
according to it
*/
std::string last_part_of_name = name.substr(last_separator_pos + 1);
auto last_part_of_name = name.substr(last_separator_pos + 1);
/*
If this name is enclosed in parentheses, generate it
and blit it onto the base image
*/
if (last_part_of_name[0] == paren_open
&& last_part_of_name[last_part_of_name.size() - 1] == paren_close) {
std::string name2 = last_part_of_name.substr(1,
&& last_part_of_name.back() == paren_close) {
auto name2 = last_part_of_name.substr(1,
last_part_of_name.size() - 2);
video::IImage *tmp = generateImage(name2, source_image_names);
if (!tmp) {
@ -1199,7 +1200,7 @@ void blitBaseImage(video::IImage* &src, video::IImage* &dst)
} \
} while(0)
bool TextureSource::generateImagePart(std::string part_of_name,
bool TextureSource::generateImagePart(std::string_view part_of_name,
video::IImage *& baseimg, std::set<std::string> &source_image_names)
{
const char escape = '\\'; // same as in generateImage()
@ -1216,8 +1217,9 @@ bool TextureSource::generateImagePart(std::string part_of_name,
// Stuff starting with [ are special commands
if (part_of_name.empty() || part_of_name[0] != '[') {
source_image_names.insert(part_of_name);
video::IImage *image = m_sourcecache.getOrLoad(part_of_name);
std::string part_s(part_of_name);
source_image_names.insert(part_s);
video::IImage *image = m_sourcecache.getOrLoad(part_s);
if (!image) {
// Do not create the dummy texture
if (part_of_name.empty())
@ -1516,8 +1518,10 @@ bool TextureSource::generateImagePart(std::string part_of_name,
return false;
}
str_replace(part_of_name, '&', '^');
Strfnd sf(part_of_name);
std::string part_s(part_of_name);
str_replace(part_s, '&', '^');
Strfnd sf(part_s);
sf.next("{");
std::string imagename_top = sf.next("{");
std::string imagename_left = sf.next("{");
@ -1873,7 +1877,7 @@ bool TextureSource::generateImagePart(std::string part_of_name,
else if (str_starts_with(part_of_name, "[png:")) {
std::string png;
{
std::string blob = part_of_name.substr(5);
auto blob = part_of_name.substr(5);
if (!base64_is_valid(blob)) {
errorstream << "generateImagePart(): "
<< "malformed base64 in [png" << std::endl;
@ -2433,7 +2437,7 @@ void brighten(video::IImage *image)
}
}
u32 parseImageTransform(const std::string& s)
u32 parseImageTransform(std::string_view s)
{
int total_transform = 0;