Skip to content

Commit

Permalink
update Scala 3.6.2, Native 0.5.6
Browse files Browse the repository at this point in the history
  • Loading branch information
spamegg1 committed Jan 19, 2025
1 parent b2d9f3e commit b46ea07
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
6 changes: 3 additions & 3 deletions project.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//> using scala 3.5.2
//> using scala 3.6.2
//> using platform native
//> using nativeVersion 0.5.5
//> using nativeVersion 0.5.6
//> using exclude "gatling/*"
//> using options -explain-cyclic -Ydebug-cyclic
//> using dep io.argonaut::argonaut::6.3.10
//> using dep io.gatling:gatling-app:3.12.0
//> using dep io.gatling:gatling-app:3.13.1
// // > using dep biz.enef:slogging_2.13:0.6.2
2 changes: 1 addition & 1 deletion scripts/installGatling.sc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env -S scala-cli
//> using dep com.lihaoyi::os-lib:0.10.7
//> using dep com.lihaoyi::os-lib:0.11.3

// download Gatling Highcharts Bundle
println("Downloading Gatling 3.10.5 to directory ./gatling")
Expand Down
29 changes: 17 additions & 12 deletions src/main/scala/ch01/10testing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,30 @@ package ch01
package testNullTermination

import scalanative.unsigned.UnsignedRichInt
import scalanative.unsafe.{CQuote, CString, CSize, Ptr, CChar, sizeof}
import scalanative.unsafe.{Zone, toCString, CQuote, CString, CSize, Ptr, CChar, sizeof}
import scalanative.libc.{string, stdio, stdlib}

// Testing how malloc, strncpy work, how to handle null termination

def generateHtml(text: String)(using Zone) =
toCString(s"<h1>$text</h1>\n")

@main
def run: Unit =
val cString: CString = c"hello" // uses CQuote
val strLen: CSize = string.strlen(cString) // 5
val buffer: Ptr[Byte] = stdlib.malloc(strLen + 1.toUSize) // 0.5
def run: Unit = Zone:
stdio.printf(generateHtml("hello"))
// val cString: CString = c"hello" // uses CQuote
// val strLen: CSize = string.strlen(cString) // 5
// val buffer: Ptr[Byte] = stdlib.malloc(strLen + 1.toUSize) // 0.5

// buffer(strLen) = 123.toByte // we can "update" a location like this
// // buffer(strLen) = 123.toByte // we can "update" a location like this

// string.strncpy(buffer, cString, strLen) // copy, excluding \0
string.strncpy(buffer, cString, strLen + 1.toUSize) // 0.5: copy, including \0
buffer(strLen) = 0.toByte // if we want to be super safe with \0
// // string.strncpy(buffer, cString, strLen) // copy, excluding \0
// string.strncpy(buffer, cString, strLen + 1.toUSize) // 0.5: copy, including \0
// buffer(strLen) = 0.toByte // if we want to be super safe with \0

for offset <- 0 until strLen.toInt + 1 do // let's check null-termination!
val chr: CChar = buffer(offset) // pointer arithmetic = array lookup
stdio.printf(c"'%c' is %d bytes long, has binary value %d\n", chr, sizeof[CChar], chr)
// for offset <- 0 until strLen.toInt + 1 do // let's check null-termination!
// val chr: CChar = buffer(offset) // pointer arithmetic = array lookup
// stdio.printf(c"'%c' is %d bytes long, has binary value %d\n", chr, sizeof[CChar], chr)

// 'h' is 1 bytes long, has binary value 104
// 'e' is 1 bytes long, has binary value 101
Expand Down

0 comments on commit b46ea07

Please sign in to comment.