News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

string builder for MASM

Started by thomas_remkus, April 03, 2008, 03:43:32 AM

Previous topic - Next topic

thomas_remkus

Is there a header for MASM that includes procs for performance string building? So you can create one byte array and append strings and numerics alike with simple to call methods? I'd like to know if one already exists because I'm looking at creating such a thing.

thanks!

hutch--

thomas,

what are you looking for, something like basic dynamic string ?

These things can be done but its slow in comparison to fixed buffers as it involves repeated memory allocation calls. With a fixed buffer size, you only need to make one memory allocation call and chop it up into the bits you require with a pointer array where with dynamic string, you create the array of pointers then allocate a string space to every member of that pointer array.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

zooba

I had a plan for one once, but I never got around to doing it.

Basically it was going to be a linked-list of strings with a function to collate the entire list into a single string. Reallocating a buffer each time you concatenate can be very slow, whereas once all the strings are known a single buffer can be allocated based on the total length of the final string. Then you just need a fast unaligned string copy operation, which you can probably find in the Laboratory.

Cheers,

Zooba :U

thomas_remkus

What I have now is a header that I'm using in c/c++ (just recently created from a class version I'm using) that allows me to keep around a minimum set of data (a struct) while providing functions to add more data to my string. Some of the "power" that I've had with this header is that it will allocate more space than needed as a guess (if it runs out of space it will just double it) and will allocate more space as needed.

Because the struct is a known type to all the functions it becomes more universal. Each function knows to search out the "length" as it does not need to be calculated for each use. Some missing functions are:


  • bsz_tolower
  • bsz_toupper
  • bsz_urlEncode
  • bsz_urlDecode
  • bsz_base64encode
  • bsz_base64decode
  • bsz_trim

But I do have it setup to where you can add some buffer space at the end of your string and call an API most standard windows APIs and have the buffer filled with your value. This really saves on additional allocations. The struct's "string" member is just a "char*" so basically everything accepts BuilderSZ.

HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
BuilderSZ bsz;

bsz_init(&bsz);
bsz_sz(&bsz, "My current directory is:\r\n\t");

bsz_makespace(&bsz, MAX_PATH + 1);
bsz.makespace = GetCurrentDirectory(bsz.makespace, bsz.current);
bsz_fixupByBuffer(&bsz, bsz.makespace);

bsz_sz(&bsz, "\r\nBut my windows directory is:\r\n\t");

bsz_makespace(&bsz, MAX_PATH + 1);
bsz.makespace = GetWindowsDirectory(bsz.current, bsz.makespace);
bsz_fixupByBuffer(&bsz, bsz.makespace);

bsz_tolower(&bsz);
bsz_rt(&bsz);
bsz_rt(&bsz);

WriteFile(hStdOut, bsz.string, bsz.length, NULL, NULL);

bsz_free(&bsz);

HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
BuilderSZ bsz;

bsz_init(&bsz);
bsz_sz(&bsz, "My current directory is:\r\n\t");

bsz_makespace(&bsz, MAX_PATH + 1);
bsz.makespace = GetCurrentDirectory(bsz.makespace, bsz.current);
bsz_fixupByBuffer(&bsz, bsz.makespace);

bsz_sz(&bsz, "\r\nBut my windows directory is:\r\n\t");

bsz_makespace(&bsz, MAX_PATH + 1);
bsz.makespace = GetWindowsDirectory(bsz.current, bsz.makespace);
bsz_fixupByBuffer(&bsz, bsz.makespace);

bsz_tolower(&bsz);
bsz_rt(&bsz);
bsz_rt(&bsz);

WriteFile(hStdOut, bsz.string, bsz.length, NULL, NULL);

bsz_free(&bsz);


I have attached a sample project. My plan was to just translate this into MASM using the "sz*" procs and "StrLen". For C/C++ the std::string is nice but horribly slow and this simple series has proven to be much faster. If anyone has a suggestion please let me know.

[attachment deleted by admin]