I have a basic question about scripts. I usually use javascript variables for buffering input and output data, but I have noticed that many of the scripts submitted to the forum by Mofi and others make extensive use of the clipboard(s) instead of variables. I'm sure there must be a reason. Are clipboards faster? Do they use memory more efficiently?
I'm using clipboards instead of variables for large strings (= large blocks, entire file) for following reasons:
- Writing a large string (in range of some MB) to a file with UltraEdit.activeDocument.write takes extremly long time while using UltraEdit.activeDocument.paste just needs a few milliseconds even for a string with dozens of MB.
I reported this to IDM by email with 2 scripts which demonstrate the difference on speed writing the same very large string into a file. I have not analyzed up to now what is the limit for fast write versus slow write depending on string length. I'm quite sure that a string with up to 64 KB is written fast also with the write command.
- I found out with a special test script creating a string with characters in range from decimal 33 to decimal 255 (Windows 1252 code page) in a table style that some characters (for example euro symbol with code value 128) and some special 2 char sequences are not written into the file as stored in memory when using command write while using the clipboard and pasting the string produces the correct result in the file. This is a mixed problem of code page and UltraEdit's own code. In "normal" text files such characters respectively 2 char sequences do not exist.
- JavaScript strings can be NULL terminated 'C' strings or Unicode strings using wide characters. But I have not yet found any documentation how to force the JavaScript core engine to define a string variable as char or wchar (wide character) string. The clipboards support both and on copy/paste the right char type is automatically selected with correct conversion depending on encoding of the file. Therefore I use the clipboards whenever I don't know if the files modified by the script are ASCII/ANSI files or Unicode files. Using UltraEdit.activeDocument.selection results always in a conversion from Unicode to ANSI on execution of a Unicode file. And UltraEdit.activeDocument.write converts the string in memory always first to ANSI in case of being internally in memory a Unicode string (defined from a clipboard) before writing the string into the file independent of the encoding of the file.
- And last there is a memory management problem on using UltraEdit.activeDocument.selection as the selected string in memory is not removed by UltraEdit from memory if the selection in the document is canceled. The memory needed for every selected string and accessed from within the script using UltraEdit.activeDocument.selection is released on script termination. This memory management issue with UltraEdit.activeDocument.selection is no problem on working with small files of some KB, but is a big problem on working on a file with hundreds of MB and making selections of very large blocks as the script during execution occupies more and more RAM up to the 2 GB limit of a 32-bit application.
Best regards from an UC/UE/UES for Windows user from Austria
Thanks, Mofi. I learned a lot from your post.
FWIW, yesterday I wrote a 1.3 MB file from a string variable and it was extremely fast, so it at least works for files of that size. I also closed the input file immediately after selectAll and reading the selection; I don't know if closing the file releases the selection memory or not.
FWIW, yesterday I wrote a 1.3 MB file from a string variable and it was extremely fast, so it at least works for files of that size. I also closed the input file immediately after selectAll and reading the selection; I don't know if closing the file releases the selection memory or not.