A gap buffer implementation in Go.
at main 395 lines 11 kB view raw
1// SPDX-FileCopyrightText: Copyright 2024 Roland Csaszar 2// SPDX-License-Identifier: MIT 3// 4// Project: go-gap-buffer 5// File: example_test.go 6// Date: 07.Feb.2024 7// 8// ============================================================================= 9 10package gapbuffer_test 11 12import ( 13 "fmt" 14 15 gap "codeberg.org/Release-Candidate/go-gap-buffer" 16) 17 18func ExampleNew() { 19 // Create a new, empty gap buffer. 20 gapBuffer := gap.New() 21 22 // Print the content of the gap buffer as a single string. 23 fmt.Println(gapBuffer.String()) 24 // Output: 25} 26 27func ExampleNewCap() { 28 // Create a new, empty gap buffer with a capacity of 10 bytes. 29 gapBuffer := gap.NewCap(10) 30 31 // Print the content size of the gap buffer in bytes. 32 fmt.Println(gapBuffer.Size()) 33 // Output: 10 34} 35 36func ExampleNewStr() { 37 // Create a new gap buffer containing "Hello, World!". 38 gapBuffer := gap.NewStr("Hello, World!") 39 40 // Print the content of the gap buffer as a single string. 41 fmt.Println(gapBuffer.String()) 42 // Output: Hello, World! 43} 44 45func ExampleNewStrCap() { 46 // Create a new gap buffer containing "Hello, World!" with a capacity of 10 47 // bytes. 48 // But "Hello, World!" is 13 bytes long, so the gap buffer's size will be 49 // grown. 50 gapBuffer := gap.NewStrCap("Hello, World!", 10) 51 52 // Print the content size of the gap buffer in bytes. 53 fmt.Println(gapBuffer.Size()) 54 // Output: 26 55} 56 57func ExampleGapBuffer_Col() { 58 // Create a new gap buffer containing the two lines "Hello, World!" and 59 // "My name is 阿保昭則." 60 gapBuffer := gap.NewStr("Hello, World!\nMy name is 阿保昭則.") 61 62 // Get the current column position of the cursor in bytes. 63 // Numbering starts at column 1! 64 col := gapBuffer.Col() 65 66 // Print the current column position of the cursor in bytes, which is 24. 67 // The string "My name is 阿保昭則." is 24 bytes long, but contains 16 unicode 68 // runes. 69 fmt.Println(col) 70 // Output: 24 71} 72 73func ExampleGapBuffer_RuneCol() { 74 // Create a new gap buffer containing the two lines "Hello, World!" and 75 // "My name is 阿保昭則." 76 gapBuffer := gap.NewStr("Hello, World!\nMy name is 阿保昭則.") 77 78 // Get the current column position of the cursor in runes. 79 // Numbering starts at column 1! 80 runeCol := gapBuffer.RuneCol() 81 82 // Print the current column position of the cursor in runes, which is 16. 83 // The string "My name is 阿保昭則." is 24 bytes long, but contains 16 unicode 84 // runes. 85 fmt.Println(runeCol) 86 // Output: 16 87} 88 89func ExampleGapBuffer_UpMv() { 90 // Create a new gap buffer containing the two lines "Hello, World!" and 91 // "My name is John." 92 gapBuffer := gap.NewStr("Hello, World!\nMy name is John.") 93 94 // Move the cursor up one line. 95 gapBuffer.UpMv() 96 97 // Get the part of the buffer before - to the left - and after - to the 98 // right - of the current cursor position. 99 l, r := gapBuffer.StringPair() 100 101 // l should be "Hello, World!" and r should be "\nMy name is John." 102 fmt.Printf("left: '%s' |cursor| right: '%s'\n", l, r) 103 // Output: left: 'Hello, World!' |cursor| right: ' 104 // My name is John.' 105} 106 107func ExampleGapBuffer_DownMv() { 108 // Create a new gap buffer containing the two lines "Hello, World!" and 109 // "My name is John." 110 gapBuffer := gap.NewStr("Hello, World!\nMy name is John.") 111 112 // Move the cursor up one line. 113 gapBuffer.UpMv() 114 115 // And move down again, we are were we started. 116 gapBuffer.DownMv() 117 118 // Get the part of the buffer before - to the left - and after - to the 119 // right - of the current cursor position. 120 l, r := gapBuffer.StringPair() 121 122 // l should be "Hello, World!\nMy name is John." and r should be empty. 123 fmt.Printf("left: '%s' |cursor| right: '%s'\n", l, r) 124 // Output: left: 'Hello, World! 125 // My name is John.' |cursor| right: '' 126} 127 128func ExampleGapBuffer_Insert() { 129 // Create a new gap buffer containing "Hello, World!". 130 gapBuffer := gap.NewStr("Hello, World!") 131 132 // Insert " My name is John." at the current position. 133 gapBuffer.Insert(" My name is John.") 134 135 // Print the content of the gap buffer as a single string. 136 fmt.Println(gapBuffer.String()) 137 // Output: Hello, World! My name is John. 138} 139 140func ExampleGapBuffer_LeftDel() { 141 // Create a new gap buffer containing the two lines "Hello, World!" and 142 // "My name is John." 143 gapBuffer := gap.NewStr("Hello, World!\nMy name is John.") 144 145 // Delete 16 runes to the left of the cursor (like backspace). 146 gapBuffer.LeftDel() 147 gapBuffer.LeftDel() 148 gapBuffer.LeftDel() 149 gapBuffer.LeftDel() 150 gapBuffer.LeftDel() 151 gapBuffer.LeftDel() 152 gapBuffer.LeftDel() 153 gapBuffer.LeftDel() 154 gapBuffer.LeftDel() 155 gapBuffer.LeftDel() 156 gapBuffer.LeftDel() 157 gapBuffer.LeftDel() 158 gapBuffer.LeftDel() 159 gapBuffer.LeftDel() 160 gapBuffer.LeftDel() 161 gapBuffer.LeftDel() 162 163 // Print the content of the gap buffer as a single string. 164 fmt.Println(gapBuffer.String()) 165 // Output: Hello, World! 166} 167 168func ExampleGapBuffer_LeftMv() { 169 // Create a new gap buffer containing the two lines "Hello, World!" and 170 // "My name is John." 171 gapBuffer := gap.NewStr("Hello, World!\nMy name is John.") 172 173 // Move 16 runes to the left of the cursor. 174 gapBuffer.LeftMv() 175 gapBuffer.LeftMv() 176 gapBuffer.LeftMv() 177 gapBuffer.LeftMv() 178 gapBuffer.LeftMv() 179 gapBuffer.LeftMv() 180 gapBuffer.LeftMv() 181 gapBuffer.LeftMv() 182 gapBuffer.LeftMv() 183 gapBuffer.LeftMv() 184 gapBuffer.LeftMv() 185 gapBuffer.LeftMv() 186 gapBuffer.LeftMv() 187 gapBuffer.LeftMv() 188 gapBuffer.LeftMv() 189 gapBuffer.LeftMv() 190 191 // Get the part of the buffer before - to the left - and after - to the 192 // right - of the current cursor position. 193 l, r := gapBuffer.StringPair() 194 195 // l should be "Hello, World!" and r should be '\nMy name is John.'. 196 fmt.Printf("left: '%s' |cursor| right: '%s'\n", l, r) 197 // Output: left: 'Hello, World! 198 // ' |cursor| right: 'My name is John.' 199} 200 201func ExampleGapBuffer_Line() { 202 // Create a new gap buffer containing the two lines "Hello, World!" and 203 // "My name is John." 204 gapBuffer := gap.NewStr("Hello, World!\nMy name is John.") 205 206 // Get the current line position of the cursor. 207 // Numbering starts at line 1! 208 line := gapBuffer.Line() 209 210 // Print the current line position of the cursor. 211 fmt.Println(line) 212 // Output: 2 213} 214 215func ExampleGapBuffer_LineCol() { 216 // Create a new gap buffer containing the two lines "Hello, World!" and 217 // "My name is 阿保昭則." 218 gapBuffer := gap.NewStr("Hello, World!\nMy name is 阿保昭則.") 219 220 // Get the current line and column position in bytes of the cursor. 221 // Numbering starts at line 1 and column 1! 222 line, col := gapBuffer.LineCol() 223 224 // Print the current line position of the cursor. 225 fmt.Printf("Line: %d column: %d", line, col) 226 // Output: Line: 2 column: 24 227} 228 229func ExampleGapBuffer_LineRuneCol() { 230 // Create a new gap buffer containing the two lines "Hello, World!" and 231 // "My name is 阿保昭則." 232 gapBuffer := gap.NewStr("Hello, World!\nMy name is 阿保昭則.") 233 234 // Get the current column position of the cursor in runes. 235 // Numbering starts at column 1! 236 line, col := gapBuffer.LineRuneCol() 237 238 // Print the current line position of the cursor. 239 fmt.Printf("Line: %d column: %d", line, col) 240 // Output: Line: 2 column: 16 241} 242 243func ExampleGapBuffer_LineLength() { 244 // Create a new gap buffer containing "Hello, Wôrld!" 245 gapBuffer := gap.NewStr("Hello, Wôrld!") 246 247 // Get the length of the current line in bytes. 248 length := gapBuffer.LineLength() 249 250 // Print the length of the current line in bytes. 251 // The string "Hello, Wôrld!" is 14 bytes long, but contains 13 unicode 252 // runes. 253 fmt.Println(length) 254 // Output: 14 255} 256 257func ExampleGapBuffer_RightMv() { 258 // Create a new gap buffer containing the two lines "Hello, World!" and 259 // "My name is John." 260 gapBuffer := gap.NewStr("Hello, World!\nMy name is John.") 261 262 // Move 16 runes to the left of the cursor. 263 gapBuffer.LeftMv() 264 gapBuffer.LeftMv() 265 gapBuffer.LeftMv() 266 gapBuffer.LeftMv() 267 gapBuffer.LeftMv() 268 gapBuffer.LeftMv() 269 gapBuffer.LeftMv() 270 gapBuffer.LeftMv() 271 gapBuffer.LeftMv() 272 gapBuffer.LeftMv() 273 gapBuffer.LeftMv() 274 gapBuffer.LeftMv() 275 gapBuffer.LeftMv() 276 gapBuffer.LeftMv() 277 gapBuffer.LeftMv() 278 gapBuffer.LeftMv() 279 280 // Move 8 runes to the right. 281 gapBuffer.RightMv() 282 gapBuffer.RightMv() 283 gapBuffer.RightMv() 284 gapBuffer.RightMv() 285 gapBuffer.RightMv() 286 gapBuffer.RightMv() 287 gapBuffer.RightMv() 288 gapBuffer.RightMv() 289 290 // Get the part of the buffer before - to the left - and after - to the 291 // right - of the current cursor position. 292 l, r := gapBuffer.StringPair() 293 294 // l should be "Hello, World!\nMy name " and r should be 'is John.'. 295 fmt.Printf("left: '%s' |cursor| right: '%s'\n", l, r) 296 // Output: left: 'Hello, World! 297 // My name ' |cursor| right: 'is John.' 298} 299 300func ExampleGapBuffer_RightDel() { 301 // Create a new gap buffer containing the two lines "Hello, World!" and 302 // "My name is John." 303 gapBuffer := gap.NewStr("Hello, World!\nMy name is John.") 304 305 // Move 5 runes to the left of the cursor. 306 gapBuffer.LeftMv() 307 gapBuffer.LeftMv() 308 gapBuffer.LeftMv() 309 gapBuffer.LeftMv() 310 gapBuffer.LeftMv() 311 312 // Delete 5 runes to the right, like the "delete" key does. 313 gapBuffer.RightDel() 314 gapBuffer.RightDel() 315 gapBuffer.RightDel() 316 gapBuffer.RightDel() 317 gapBuffer.RightDel() 318 319 // Get the part of the buffer before - to the left - and after - to the 320 // right - of the current cursor position. 321 l, r := gapBuffer.StringPair() 322 323 // l should be "Hello, World!\nMy name is" and r should be empty. 324 fmt.Printf("left: '%s' |cursor| right: '%s'\n", l, r) 325 // Output: left: 'Hello, World! 326 // My name is ' |cursor| right: '' 327} 328 329func ExampleGapBuffer_Size() { 330 // Create a new gap buffer containing the two lines "Hello, World!" and 331 // "My name is 阿保昭則." 332 gapBuffer := gap.NewStr("Hello, World!\nMy name is 阿保昭則.") 333 334 // Get the current size in bytes of the gap buffer. 335 // This includes (unused bytes in) the gap 336 size := gapBuffer.Size() 337 338 // Print the current line position of the cursor. 339 fmt.Println(size) 340 // Output: 1024 341} 342 343func ExampleGapBuffer_String() { 344 // Create a new gap buffer containing "Hello, World!". 345 gapBuffer := gap.NewStr("Hello, World!") 346 347 // Print the content of the gap buffer as a single string. 348 fmt.Println(gapBuffer.String()) 349 // Output: Hello, World! 350} 351 352func ExampleGapBuffer_StringLength() { 353 // Create a new gap buffer containing "Hello, World!". 354 gapBuffer := gap.NewStr("Hello, World!") 355 356 // Get the length of the content of the gap buffer in bytes. 357 length := gapBuffer.StringLength() 358 359 // Print the length of the content of the gap buffer in bytes. 360 fmt.Println(length) 361 // Output: 13 362} 363 364func ExampleGapBuffer_StringPair() { 365 // Create a new gap buffer containing the two lines "Hello, World!" and 366 // "My name is John." 367 gapBuffer := gap.NewStr("Hello, World!\nMy name is John.") 368 369 // Move 16 runes to the left of the cursor. 370 gapBuffer.LeftMv() 371 gapBuffer.LeftMv() 372 gapBuffer.LeftMv() 373 gapBuffer.LeftMv() 374 gapBuffer.LeftMv() 375 gapBuffer.LeftMv() 376 gapBuffer.LeftMv() 377 gapBuffer.LeftMv() 378 gapBuffer.LeftMv() 379 gapBuffer.LeftMv() 380 gapBuffer.LeftMv() 381 gapBuffer.LeftMv() 382 gapBuffer.LeftMv() 383 gapBuffer.LeftMv() 384 gapBuffer.LeftMv() 385 gapBuffer.LeftMv() 386 387 // Get the part of the buffer before - to the left - and after - to the 388 // right - of the current cursor position. 389 l, r := gapBuffer.StringPair() 390 391 // l should be "Hello, World!" and r should be '\nMy name is John.'. 392 fmt.Printf("left: '%s' |cursor| right: '%s'\n", l, r) 393 // Output: left: 'Hello, World! 394 // ' |cursor| right: 'My name is John.' 395}