首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何查看dart核心库代码(StringBuffer)?

如何查看dart核心库代码(StringBuffer)?
EN

Stack Overflow用户
提问于 2022-09-15 02:49:47
回答 1查看 16关注 0票数 0

我只是想知道StringBuffer的具体代码是什么样的。

但我只能找到这样的抽象方法。好吧..。

你能让我知道具体的代码吗?谢谢!

代码语言:javascript
复制
// Copyright (c) 2011, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

part of dart.core;

class StringBuffer implements StringSink {
  /// Creates a string buffer containing the provided [content].
  external StringBuffer([Object content = ""]);

  /// Returns the length of the content that has been accumulated so far.
  /// This is a constant-time operation.
  external int get length;

  /// Returns whether the buffer is empty. This is a constant-time operation.
  bool get isEmpty => length == 0;

  /// Returns whether the buffer is not empty. This is a constant-time
  /// operation.
  bool get isNotEmpty => !isEmpty;

  /// Adds the string representation of [object] to the buffer.
  external void write(Object? object);

  /// Adds the string representation of [charCode] to the buffer.
  ///
  /// Equivalent to `write(String.fromCharCode(charCode))`.
  external void writeCharCode(int charCode);

  /// Writes all [objects] separated by [separator].
  ///
  /// Writes each individual object in [objects] in iteration order,
  /// and writes [separator] between any two objects.
  external void writeAll(Iterable<dynamic> objects, [String separator = ""]);

  external void writeln([Object? obj = ""]);

  /// Clears the string buffer.
  external void clear();

  /// Returns the contents of buffer as a single string.
  external String toString();
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-15 04:58:34

这取决于您感兴趣的目标平台,这也是为什么要这样做的原因,因为external函数可以根据目标指向不同的实现。

在这里可以找到不同平台的实现:

https://github.com/dart-lang/sdk/tree/2.18.1/sdk/lib/_internal

因此,如果您希望看到在运行Dart VM或编译为AOT二进制文件时使用的实现,可以在这里找到StringBuffer的实现:

https://github.com/dart-lang/sdk/blob/2.18.1/sdk/lib/_internal/vm/lib/string_buffer_patch.dart

这里的另一点是,Dart VM代码通常有如下内容,您可以在StringBuffer实现的底部看到这些内容:

代码语言:javascript
复制
  @pragma("vm:external-name", "StringBuffer_createStringFromUint16Array")
  external static String _create(Uint16List buffer, int length, bool isLatin1);

这意味着我们调用Dart运行时中的C++代码(它与编译的应用程序捆绑在一起)。因此,_create调用最终将调用以下方法:

https://github.com/dart-lang/sdk/blob/2.18.1/runtime/lib/string.cc#L516-L534

如果您的目标是JavaScript,则可以在这里找到用于该实现的代码(当编译到发布就绪JS包时):

https://github.com/dart-lang/sdk/blob/2.18.1/sdk/lib/_internal/js_runtime/lib/core_patch.dart#L632

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73725115

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档