GithubHelp home page GithubHelp logo

Comments (6)

hydrobyte avatar hydrobyte commented on September 18, 2024

Hi,

Nice hint! That was in my plans.

I think it is simple to implement BOM support and LoadFromStream() is indeed the right place to filter the enconding.

Stay tuned.

Regards,

from mcjson.

totyaxy avatar totyaxy commented on September 18, 2024

Thank you!

from mcjson.

hydrobyte avatar hydrobyte commented on September 18, 2024

Hi, about UTF-8 BOM, please test the latest update.

from mcjson.

totyaxy avatar totyaxy commented on September 18, 2024

Hi, I saw the BOM bypass code, thank you! I would add a tip to be faster (we need the speed), because this solution will completely copy the string, even if it is very big, and this is an unnecessary operation. I think we should detect Bom as a stream, and then:

if utf8bomheaderdetect (stream) THEN
Stream.seek (Length (Cutf8bomheader), Sofromcurrent);

Because stream.read will read from Position(!).
So you just have to slip the position. Or simply set the PostionAfterBOM to TRUE in UTF8BOMHeaderDetect, my quick code for this:

type
  TUTF8BOMHeader = array [0..2] of byte;

const
  //https://en.wikipedia.org/wiki/Byte_order_mark
  cUTF8BOMHeader: TUTF8BOMHeader = ($EF, $BB, $BF);

function UTF8BOMHeaderDetect(const Stream: TStream;
  const PostionAfterBOM: boolean = False): boolean;
var
  UTF8BOMHeader: TUTF8BOMHeader;
  StreamPositionBuf: int64;
begin
  Result := False;

  Initialize(UTF8BOMHeader, Length(UTF8BOMHeader));

  if Stream.Size < Length(cUTF8BOMHeader) then
    exit;

  StreamPositionBuf := Stream.Position;
  Stream.Read(UTF8BOMHeader, Length(UTF8BOMHeader));

  if CompareMem(@UTF8BOMHeader, @cUTF8BOMHeader, Length(cUTF8BOMHeader)) then
  begin
    if not (PostionAfterBOM) then Stream.Position := StreamPositionBuf;
    Result := True;
  end
  else
    Stream.Position := StreamPositionBuf;
end;

from mcjson.

totyaxy avatar totyaxy commented on September 18, 2024

But by the way, your code also loads the UTF8 BOM encoded file, thanks!

from mcjson.

hydrobyte avatar hydrobyte commented on September 18, 2024

Nice hint about performance. I'll give it a try.

Thanks.

from mcjson.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.