Skip to content

Commit 902fbd1

Browse files
committed
Avoid duplication
1 parent a94223e commit 902fbd1

File tree

1 file changed

+74
-108
lines changed

1 file changed

+74
-108
lines changed

sdk/log/crate/src/logger.rs

Lines changed: 74 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -298,125 +298,91 @@ macro_rules! impl_log_for_unsigned_integer {
298298
let source = digits.as_ptr().add(offset);
299299
let ptr = buffer.as_mut_ptr();
300300

301-
#[cfg(target_os = "solana")]
302-
{
303-
// Copy the number to the buffer if no precision is specified.
304-
if precision == 0 {
305-
syscalls::sol_memcpy_(
306-
ptr as *mut _,
307-
source as *const _,
308-
written as u64,
309-
);
310-
}
311-
// If padding is needed to satisfy the precision, add leading zeros
312-
// and a decimal point.
313-
else if precision >= written {
314-
// Prefix and decimal point.
315-
(ptr as *mut u8).write(b'0');
316-
317-
if length > 2 {
318-
(ptr.add(1) as *mut u8).write(b'.');
319-
// Precision padding.
320-
let padding = min(length - 2, precision - written);
321-
syscalls::sol_memset(
322-
ptr.add(2) as *mut _,
323-
b'0',
324-
padding as u64,
325-
);
326-
let current = 2 + padding;
327-
328-
// If there is still space, copy (part of) the number.
329-
if current < length {
330-
let remaining = min(written, length - current);
331-
// Number part.
332-
syscalls::sol_memcpy_(
333-
ptr.add(current) as *mut _,
334-
source as *const _,
335-
remaining as u64,
336-
);
337-
}
338-
}
339-
}
340-
// No padding is needed, calculate the integer and fractional
341-
// parts and add a decimal point.
342-
else {
343-
// Integer part of the number.
344-
let integer_part = written - precision;
345-
syscalls::sol_memcpy_(
346-
ptr as *mut _,
347-
source as *const _,
348-
integer_part as u64,
301+
// Copy the number to the buffer if no precision is specified.
302+
if precision == 0 {
303+
#[cfg(target_os = "solana")]
304+
syscalls::sol_memcpy_(
305+
ptr as *mut _,
306+
source as *const _,
307+
written as u64,
308+
);
309+
#[cfg(not(target_os = "solana"))]
310+
copy_nonoverlapping(source, ptr, written);
311+
}
312+
// If padding is needed to satisfy the precision, add leading zeros
313+
// and a decimal point.
314+
else if precision >= written {
315+
// Prefix.
316+
(ptr as *mut u8).write(b'0');
317+
318+
if length > 2 {
319+
(ptr.add(1) as *mut u8).write(b'.');
320+
let padding = min(length - 2, precision - written);
321+
322+
// Precision padding.
323+
#[cfg(target_os = "solana")]
324+
syscalls::sol_memset(
325+
ptr.add(2) as *mut _,
326+
b'0',
327+
padding as u64,
349328
);
329+
#[cfg(not(target_os = "solana"))]
330+
(ptr.add(2) as *mut u8).write_bytes(b'0', padding);
350331

351-
// Decimal point.
352-
(ptr.add(integer_part) as *mut u8).write(b'.');
353-
let current = integer_part + 1;
332+
let current = 2 + padding;
354333

355-
// If there is still space, copy (part of) the remaining.
334+
// If there is still space, copy (part of) the number.
356335
if current < length {
357-
let remaining = min(precision, length - current);
358-
// Fractional part of the number.
336+
let remaining = min(written, length - current);
337+
338+
// Number part.
339+
#[cfg(target_os = "solana")]
359340
syscalls::sol_memcpy_(
360341
ptr.add(current) as *mut _,
361-
source.add(integer_part) as *const _,
342+
source as *const _,
362343
remaining as u64,
363344
);
345+
#[cfg(not(target_os = "solana"))]
346+
copy_nonoverlapping(source, ptr.add(current), remaining);
364347
}
365348
}
366349
}
367-
368-
#[cfg(not(target_os = "solana"))]
369-
{
370-
// Copy the number to the buffer if no precision is specified.
371-
if precision == 0 {
372-
copy_nonoverlapping(source, ptr, written);
373-
}
374-
// If padding is needed to satisfy the precision, add leading zeros
375-
// and a decimal point.
376-
else if precision >= written {
377-
// Prefix and decimal point.
378-
(ptr as *mut u8).write(b'0');
379-
380-
if length > 2 {
381-
(ptr.add(1) as *mut u8).write(b'.');
382-
// Precision padding.
383-
let padding = min(length - 2, precision - written);
384-
(ptr.add(2) as *mut u8).write_bytes(b'0', padding);
385-
let current = 2 + padding;
386-
387-
// If there is still space, copy (part of) the number.
388-
if current < length {
389-
let remaining = min(written, length - current);
390-
// Number part.
391-
copy_nonoverlapping(
392-
source,
393-
ptr.add(current),
394-
remaining,
395-
);
396-
}
397-
}
398-
}
399-
// No padding is needed, calculate the integer and fractional
400-
// parts and add a decimal point.
401-
else {
402-
// Integer part of the number.
403-
let integer_part = written - precision;
404-
copy_nonoverlapping(source, ptr, integer_part);
405-
406-
// Decimal point.
407-
(ptr.add(integer_part) as *mut u8).write(b'.');
408-
let current = integer_part + 1;
409-
410-
// If there is still space, copy (part of) the remaining.
411-
if current < length {
412-
let remaining = min(precision, length - current);
413-
// Fractional part of the number.
414-
copy_nonoverlapping(
415-
source.add(integer_part),
416-
ptr.add(current),
417-
remaining,
418-
);
419-
}
350+
// No padding is needed, calculate the integer and fractional
351+
// parts and add a decimal point.
352+
else {
353+
let integer_part = written - precision;
354+
355+
// Integer part of the number.
356+
#[cfg(target_os = "solana")]
357+
syscalls::sol_memcpy_(
358+
ptr as *mut _,
359+
source as *const _,
360+
integer_part as u64,
361+
);
362+
#[cfg(not(target_os = "solana"))]
363+
copy_nonoverlapping(source, ptr, integer_part);
364+
365+
// Decimal point.
366+
(ptr.add(integer_part) as *mut u8).write(b'.');
367+
let current = integer_part + 1;
368+
369+
// If there is still space, copy (part of) the remaining.
370+
if current < length {
371+
let remaining = min(precision, length - current);
372+
373+
// Fractional part of the number.
374+
#[cfg(target_os = "solana")]
375+
syscalls::sol_memcpy_(
376+
ptr.add(current) as *mut _,
377+
source.add(integer_part) as *const _,
378+
remaining as u64,
379+
);
380+
#[cfg(not(target_os = "solana"))]
381+
copy_nonoverlapping(
382+
source.add(integer_part),
383+
ptr.add(current),
384+
remaining,
385+
);
420386
}
421387
}
422388
}

0 commit comments

Comments
 (0)