[lib][v2] buf.ahk - Primitive buffer functions.

Post your working scripts, libraries and tools.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

[lib][v2] buf.ahk - Primitive buffer functions.

15 Oct 2017, 11:52

Introduction
The functions consider two buffer types, char* (1 byte elements) and short* (2 byte elements). There are three functions for each buffer type, they are find, count and write. Short summary,
  • find(), finds the first occurance of a needle in a haystack.
  • count(), counts the number of occurances of a needle in a haystack.
  • write(), finds position p of a needle in a haystack, and writes a buffer into the haystack at position p + an optional offset.
For example, you can think of the find functions as an instr function but where you specify the length to search over, rather than ending on null termination ('\0').

Documentation
Documentation is available at the github :arrow: wiki.

Download
Source available at github :arrow: buf.ahk. (Repo. includes the c source files.)

Example
String count function, counts the line breaks in a string, example for v2

Code: Select all

#include buf.ahk
strCount(byref hay, byref ned, overlap:=0){	; the corresponding ANSI function would use buf_byte_find instead.
	return buf_short_count(&hay, &ned, strlen(hay), strlen(ned), overlap)
}
; Test script:
nLines:=1000000
str:=makeStringWithManyLines(nLines)
t1:=QPC()
	nFound:=strCount(str, "`n")			; Count line breaks
t2:=QPC()

msgbox("time taken: " round(t2-t1,3) " ms.`nFound: " nFound " linebreaks.")	; Show result

makeStringWithManyLines(N:=1000000){
	static lineText:="some text "
	varsetcapacity(str,(N*(strlen(lineText)+strlen(N)))*2) 
	loop N
		str.= lineText A_Index "`n" 
	return str
}

;-------------------------------------------------------------------------------
QPC() { ; microseconds precision, borrowed from wolf_II
;-------------------------------------------------------------------------------
    static Freq, init := DllCall("QueryPerformanceFrequency", "Int64P", Freq)

    DllCall("QueryPerformanceCounter", "Int64P", Count)
    Return (Count / Freq)*1000
}
Edit: this only works for v2 now, you can browse the github history to get a v1 version or manually convert (simple).
v1
Known issues
Very limited testing.

Misc
Feel free to use this in whichever modified or non-modified state you wish, for any peaceful purpose. No credits or references needed.

Have fun and good luck :wave:

Return to “Scripts and Functions (v2)”

Who is online

Users browsing this forum: No registered users and 14 guests