67 lines
1.3 KiB
C++
67 lines
1.3 KiB
C++
|
/***
|
||
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
||
|
|
||
|
File: SJIS.hpp
|
||
|
Date: 2021-11-1
|
||
|
Author: Reece
|
||
|
***/
|
||
|
#pragma once
|
||
|
|
||
|
namespace Aurora::Locale::Encoding::SJIS
|
||
|
{
|
||
|
static inline int GetLenSJISCodePoint(const AuUInt8 *in, AuUInt32 len)
|
||
|
{
|
||
|
if (len == 0)
|
||
|
{
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
auto b = in[0];
|
||
|
|
||
|
if (b >= 0x80)
|
||
|
{
|
||
|
if (b <= 0xDF)
|
||
|
{
|
||
|
if (len < 2)
|
||
|
{
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
return 2;
|
||
|
}
|
||
|
else if (b <= 0xEF)
|
||
|
{
|
||
|
if (len < 3)
|
||
|
{
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
return 3;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
if (len < 4)
|
||
|
{
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
return 4;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
static inline AuUInt32 CountSJIS(const void *base, AuUInt32 length, bool bytes = false)
|
||
|
{
|
||
|
AuUInt32 i {}, cps {};
|
||
|
for (; i < length; )
|
||
|
{
|
||
|
auto next = GetLenSJISCodePoint((const AuUInt8 *)base + i, length - i);
|
||
|
if (next == 0) return i;
|
||
|
cps++;
|
||
|
i += next;
|
||
|
}
|
||
|
return bytes ? i : cps;
|
||
|
}
|
||
|
}
|