- 1
#define private public
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
+890.7
#define private public
−5.8
function IntToMonth(NumberMonth: Integer): string;
begin
сase NumberMonth of
1: Result := 'Январь';
2: Result := 'Февраль';
3: Result := 'Март';
4: Result := 'Апрель';
5: Result := 'Май';
6: Result := 'Июнь';
7: Result := 'Июль';
8: Result := 'Август';
9: Result := 'Сентябрь';
10: Result := 'Октябрь';
11: Result := 'Ноябрь';
12: Result := 'Декабрь';
else
Result := 'Август';
end;
end;
Думаю, по названию функции все понятно :)
+1
// https://github.com/torvalds/linux/blob/b6dad5178ceaf23f369c3711062ce1f2afc33644/rust/alloc/alloc.rs#L376
pub const fn handle_alloc_error(layout: Layout) -> ! {
const fn ct_error(_: Layout) -> ! {
panic!("allocation failed");
}
fn rt_error(layout: Layout) -> ! {
unsafe {
__rust_alloc_error_handler(layout.size(), layout.align());
}
}
unsafe { core::intrinsics::const_eval_select((layout,), ct_error, rt_error) }
}
// https://github.com/torvalds/linux/blob/b6dad5178ceaf23f369c3711062ce1f2afc33644/rust/kernel/lib.rs#L96-L103
fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
pr_emerg!("{}\n", info);
// SAFETY: FFI call.
unsafe { bindings::BUG() };
// Bindgen currently does not recognize `__noreturn` so `BUG` returns `()`
// instead of `!`. See <https://github.com/rust-lang/rust-bindgen/issues/2094>.
loop {}
}
// https://github.com/torvalds/linux/blob/master/include/asm-generic/bug.h#L51-L68
/*
* Don't use BUG() or BUG_ON() unless there's really no way out; one
* example might be detecting data structure corruption in the middle
* of an operation that can't be backed out of. If the (sub)system
* can somehow continue operating, perhaps with reduced functionality,
* it's probably not BUG-worthy.
*
* If you're tempted to BUG(), think again: is completely giving up
* really the *only* solution? There are usually better options, where
* users don't need to reboot ASAP and can mostly shut down cleanly.
*/
#ifndef HAVE_ARCH_BUG
#define BUG() do { \
printk("BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \
barrier_before_unreachable(); \
panic("BUG!"); \
} while (0)
#endif
О том, как в ядре Linux говнораст обрабатывает ошибку аллокации
−4
Зачем простенькие проги, написанные на MASM коннектятся к ip адресу Microsoft 20.99.133.109:443?
−2
cli
a: jmp a
0
int main() {
ifstream input_file;
input_file.open("DATA");
string line;
while (getline(input_file, line)) {
size_t i = 0, j = 0;
string fname, sname;
float score;
char c = line[0];
while((c != ' ') && (j < line.size())) {
c = line[j];
j++;
}
fname = line.substr(i, j - i - 1);
i = j;
j += 1;
c = line[i];
while((c != ' ') && (j < line.size())) {
c = line[j];
j++;
}
sname = line.substr(i, j - i - 1);
i = j;
j += 1;
score = stof(line.substr(i, line.size()));
Data dat;
dat.fname = fname;
dat.sname = sname;
dat.score = score;
vec.push_back(dat);
}
const auto comp_fname = [](Data a, Data b){return a.fname >= b.fname;};
const auto comp_sname = [](Data a, Data b){return a.sname >= b.sname;};
const auto comp_score = [](Data a, Data b){return a.score >= b.score;};
const auto dcomp_fname = [](Data a, Data b){return a.fname < b.fname;};
const auto dcomp_sname = [](Data a, Data b){return a.sname < b.sname;};
const auto dcomp_score = [](Data a, Data b){return a.score < b.score;};
print(vec);
char choice = 0, order = 0;
cout << "Как сортировать (0 - Fфмилия, 1 - Iмя, 2 - CpegHuu_6aJlJl): ";
cin >> choice;
cout << "А в какмо порядке?7? (0 - по убыванию, 1 - по возрастанию)Ж ";
cin >> order;
if (choice == '0') {
if (order == '0') {
sort(vec.begin(), vec.end(), comp_fname);
} else {
sort(vec.begin(), vec.end(), dcomp_fname);
}
} else if (choice == '1') {
if (order == '0') {
sort(vec.begin(), vec.end(), comp_sname);
} else {
sort(vec.begin(), vec.end(), dcomp_sname);
}
} else if (choice == '2') {
if (order == '0') {
sort(vec.begin(), vec.end(), comp_score);
} else {
sort(vec.begin(), vec.end(), dcomp_score);
}
}
// и т.д. ...
Как вам? Зачёт? Незачёт? Удовлетворительно?
−1
class Node<T> {
v: T;
k: string;
next: Node<T>;
}
class Map<T> {
head: Node<T>;
getElt(k: string): T {
return mapGet(this, k)
}
setElt(k: string, v: T) {
mapSet(this, k, v)
}
}
function mapSet<T>(m: Map<T>, k: string, v: T) {
for (let p = m.head; p != null; p = p.next) {
if (p.k == k) {
p.v = v
return
}
}
let n = new Node<T>()
n.next = m.head
n.k = k
n.v = v
m.head = n
}
function mapGet<T>(m: Map<T>, k: string): T {
for (let p = m.head; p != null; p = p.next) {
if (p.k == k) {
return p.v
}
}
return null
}
function search_array<T>(a: T[], item: T): number {
for (let i = 0; i < a.length; i++) {
if (a[i] == item) {
return i
}
}
return -1 // NOT FOUND
}
class MyMap<K, V> {
keys: K[]
values: V[]
constructor() {
this.keys = []
this.values = []
}
push(key: K, value: V) {
this.keys.push(key)
this.values.push(value)
}
value_for(key: K): V {
let i = search_array(this.keys, key)
if (i == -1) {
return null
}
return this.values[i]
}
key_for(value: V): K {
let i = search_array(this.values, value)
if (i == -1) {
return null
}
return this.keys[i]
}
set(key: K, value: V): void {
let i = search_array(this.keys, key)
if (i == -1) {
this.keys.push(key)
this.values.push(value)
} else {
this.values[i] = value
}
}
has_key(key: K): boolean {
return search_array(this.keys, key) != -1
}
has_value(value: V): boolean {
return search_array(this.values, value) != -1
}
}
Срочно нужна помощь... не могу решить что делать с функцией mapGet .. когда T - number я не могу использовать "null" .. должен я хакнуть компилятор или изменить тестовый пример?
0
class Node<T> {
v: T;
k: string;
next: Node<T>;
}
function main()
{
let n = new Node<number>()
n.next = n
n.k = "Hello";
n.v = 10.0;
print("done.");
}
Вы не поверите как сложно сделать простые вещи в LLVM. встречаем рекурсивные типы :)
+3
template<typename T>
static ALWAYS_INLINE void FormatLogMessageAndPrintW(
const char* channelName,
const char* functionName,
LOGLEVEL level,
const char* message,
bool timestamp,
bool ansi_color_code,
bool newline,
const T& callback)
{
char buf[512];
char* message_buf = buf;
int message_len;
if ((message_len = FormatLogMessageForDisplay(message_buf,
sizeof(buf), channelName, functionName,
level,
message, timestamp,
ansi_color_code, newline)) > (sizeof(buf) - 1))
{
message_buf = static_cast<char*>(std::malloc(message_len + 1));
message_len = FormatLogMessageForDisplay(message_buf,
message_len + 1, channelName, functionName,
level, message, timestamp, ansi_color_code, newline);
}
if (message_len <= 0)
return;
// Convert to UTF-16 first so unicode characters display correctly.
// NT is going to do it anyway...
wchar_t wbuf[512];
wchar_t* wmessage_buf = wbuf;
int wmessage_buflen = countof(wbuf) - 1;
if (message_len >= countof(wbuf))
{
wmessage_buflen = message_len;
wmessage_buf = static_cast<wchar_t*>
(std::malloc((wmessage_buflen + 1) * sizeof(wchar_t)));
}
wmessage_buflen = MultiByteToWideChar(CP_UTF8, 0, message_buf,
message_len, wmessage_buf, wmessage_buflen);
if (wmessage_buflen <= 0)
return;
wmessage_buf[wmessage_buflen] = '\0';
callback(wmessage_buf, wmessage_buflen);
if (wmessage_buf != wbuf)
{
std::free(wbuf); // <=
}
if (message_buf != buf)
{
std::free(message_buf);
}
}
Отсюда:
https://pvs-studio.com/ru/blog/posts/cpp/0880/
+5
private static String getMargin(final int size) {
return " ".substring(0, 6 * size);
}
Как создать пустую строку с заданной длиной...