Clipboard vs Variables in Scripts

Clipboard vs Variables in Scripts

133
Basic UserBasic User
133

    Oct 31, 2013#1

    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?

    6,603548
    Grand MasterGrand Master
    6,603548

      Oct 31, 2013#2

      I'm using clipboards instead of variables for large strings (= large blocks, entire file) for following reasons:
      1. 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.
      2. 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.
      3. 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.
      4. 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.
      In other words for approximately 99.5% of all UltraEdit scripts usage of a clipboard instead of a string variable does not make a difference. But for the small rest of all scripts or when not knowing on which files (size, encoding) a script is designed for, it makes a difference. The scripts published by me in this forum are mainly written using a clipboard as I don't know the size of the file or the character encoding used in the files of the questioner or any other person reading the forum topic and also using the script.
      Best regards from an UC/UE/UES for Windows user from Austria

      133
      Basic UserBasic User
      133

        Oct 31, 2013#3

        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.