MCPcopy Index your code
hub / github.com/nodejs/node / _fill

Function _fill

lib/buffer.js:1124–1195  ·  view source on GitHub ↗
(buf, value, offset, end, encoding)

Source from the content-addressed store, hash-verified

1122};
1123
1124function _fill(buf, value, offset, end, encoding) {
1125 if (typeof value === 'string') {
1126 if (offset === undefined || typeof offset === 'string') {
1127 encoding = offset;
1128 offset = 0;
1129 end = buf.length;
1130 } else if (typeof end === 'string') {
1131 encoding = end;
1132 end = buf.length;
1133 }
1134
1135 const normalizedEncoding = normalizeEncoding(encoding);
1136 if (normalizedEncoding === undefined) {
1137 validateString(encoding, 'encoding');
1138 throw new ERR_UNKNOWN_ENCODING(encoding);
1139 }
1140
1141 if (value.length === 0) {
1142 // If value === '' default to zero.
1143 value = 0;
1144 } else if (value.length === 1) {
1145 // Fast path: If `value` fits into a single byte, use that numeric value.
1146 // ASCII shares this branch with utf8 since code < 128 covers the full
1147 // ASCII range; anything outside falls through to C++ bindingFill.
1148 if (normalizedEncoding === 'utf8' || normalizedEncoding === 'ascii') {
1149 const code = StringPrototypeCharCodeAt(value, 0);
1150 if (code < 128) {
1151 value = code;
1152 }
1153 } else if (normalizedEncoding === 'latin1') {
1154 value = StringPrototypeCharCodeAt(value, 0);
1155 }
1156 }
1157 } else {
1158 encoding = undefined;
1159 }
1160
1161 if (offset === undefined) {
1162 offset = 0;
1163 end = buf.length;
1164 } else {
1165 validateOffset(offset, 'offset');
1166 // Invalid ranges are not set to a default, so can range check early.
1167 if (end === undefined) {
1168 end = buf.length;
1169 } else {
1170 validateOffset(end, 'end', 0, buf.length);
1171 }
1172 if (offset >= end)
1173 return buf;
1174 }
1175
1176
1177 if (typeof value === 'number') {
1178 // OOB check
1179 const byteLen = TypedArrayPrototypeGetByteLength(buf);
1180 const fillLength = end - offset;
1181 if (offset > end || fillLength + offset > byteLen)

Callers 1

buffer.jsFile · 0.85

Calls 2

normalizeEncodingFunction · 0.85
validateOffsetFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…