Skip to content

Commit

Permalink
api: update string literal handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Aug 6, 2024
1 parent a601acb commit db9ab1a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
4 changes: 2 additions & 2 deletions api/env.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ namespace jule
const LPWSTR warg = argvw[i];
args._slice[i] = jule::utf16_to_utf8_str(warg, std::wcslen(warg));
#else
args._slice[i] = jule::argv[i];
args._slice[i] = jule::Str::lit(jule::argv[i], std::strlen(jule::argv[i]));
#endif
}
#ifdef OS_WINDOWS
Expand Down Expand Up @@ -91,7 +91,7 @@ namespace jule
#else
char **it = jule::envp;
for (; *it != NULL; ++it)
env.push(jule::Str(*it));
env.push(jule::Str(*it, std::strlen(*it)));
#endif
return env;
}
Expand Down
13 changes: 10 additions & 3 deletions api/str.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ namespace jule
return buf;
}

static jule::Str lit(const char *s, const jule::Int n) noexcept {
jule::Str str;
str.buffer = jule::Str::buffer_t::make(
const_cast<jule::U8*>(reinterpret_cast<const jule::U8*>(s)), nullptr);
str._slice = str.buffer.alloc;
str._len = n;
return str;
}

static jule::Str from_rune(const jule::I32 r) noexcept
{
jule::Str s;
Expand All @@ -78,9 +87,7 @@ namespace jule
Str(jule::Str &&src) : buffer(std::move(src.buffer)), _len(src._len), _slice(src._slice) {}
Str(const std::basic_string<jule::U8> &src) : Str(src.c_str(), src.c_str() + src.size()) {}
Str(const char *src, const jule::Int &len) : Str(reinterpret_cast<const jule::U8 *>(src), len) {}
Str(const jule::U8 *src, const jule::Int &len) : buffer(jule::Str::buffer_t::make(const_cast<jule::U8 *>(src), nullptr)),
_slice(const_cast<jule::U8 *>(src)),
_len(len) {}
Str(const jule::U8 *src, const jule::Int &len) : jule::Str(src, src + len) {}
Str(const jule::U8 *src) : Str(src, src + std::strlen(reinterpret_cast<const char *>(src))) {}
Str(const std::string &src) : Str(reinterpret_cast<const jule::U8 *>(src.c_str()),
reinterpret_cast<const jule::U8 *>(src.c_str() + src.size())) {}
Expand Down
2 changes: 1 addition & 1 deletion src/julec/obj/cxx/expr.jule
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl exprCoder {
}
len := conv::FmtInt(i64(len(content)), 10)
self.oc.write(typeCoder.Str)
self.oc.write("(")
self.oc.write("::lit(")
cstrLit(self.oc.Obj, content)
self.oc.write(", ")
self.oc.write(len)
Expand Down

0 comments on commit db9ab1a

Please sign in to comment.