From 4e9efb0b6096c35edc0b98650cf64acb367d5ba8 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Mon, 9 Aug 2021 18:18:19 +0200 Subject: Teach QByteArrayView how to parse numbers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Now that we don't need '\0'-termination on the data, this is possible. Moved QByteArray's tests to tst_QByteArrayApiSymmetry and added some more test-cases. [ChangeLog][QtCore][QByteArrayView] Added numeric parsing methods. Change-Id: Ic0df91ecfe5dbf6f008d344dd0464d7927f32273 Reviewed-by: Thiago Macieira Reviewed-by: MÃ¥rten Nordheim --- .../code/src_corelib_text_qbytearrayview.cpp | 23 +++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'src/corelib/doc/snippets/code') diff --git a/src/corelib/doc/snippets/code/src_corelib_text_qbytearrayview.cpp b/src/corelib/doc/snippets/code/src_corelib_text_qbytearrayview.cpp index d70caa2650..519f609790 100644 --- a/src/corelib/doc/snippets/code/src_corelib_text_qbytearrayview.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_text_qbytearrayview.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2020 The Qt Company Ltd. +** Copyright (C) 2021 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the documentation of the Qt Toolkit. @@ -58,3 +58,24 @@ void fun(QByteArrayView bv); void fun(char ch) { fun(QByteArrayView(&ch, 1)); } //! [1] + +//! [2] +QByteArrayView str("FF"); +bool ok; +int hex = str.toInt(&ok, 16); // hex == 255, ok == true +int dec = str.toInt(&ok, 10); // dec == 0, ok == false +//! [2] + +//! [3] +QByteArrayView str("FF"); +bool ok; +long hex = str.toLong(&ok, 16); // hex == 255, ok == true +long dec = str.toLong(&ok, 10); // dec == 0, ok == false +//! [3] + +//! [4] +QByteArrayView string("1234.56 Volt"); +bool ok; +float a = str.toFloat(&ok); // a == 0, ok == false +a = string.first(7).toFloat(&ok); // a == 1234.56, ok == true +//! [4] -- cgit v1.2.3